简体   繁体   English

如何使用 python 中 tsv 文件中的名称从一个文件夹中打开多个文件?

[英]How to open multiple files from a folder using the names in tsv file in python?

I have a metadata.tsv file like, eg.,我有一个 metadata.tsv 文件,例如,

id   group
SRR01   1
SRR02   1
SRR04   2
SRR05   2

in a directory, i have the following files: SRR01_1.fq, SRR01_2.fq, SRR02_1.fq, SRR02_2.fq, SRR04_1.fq, SRR04_2.fq, SRR05_1.fq, SRR05_2.fq在目录中,我有以下文件:SRR01_1.fq、SRR01_2.fq、SRR02_1.fq、SRR02_2.fq、SRR04_1.fq、SRR04_2.fq、SRR05_1.fq、SRR05_2.fq

Now, I need to input the files by their groups and then output the result in separate folder.现在,我需要按组输入文件,然后 output 将结果放在单独的文件夹中。

I have tried using panda and glob.我试过使用 panda 和 glob。

Maybe something along these lines:也许沿着这些路线:

import pandas

metadata = pandas.read_csv('metadata.tsv', sep= '\t')

rule all:
    input:
        expand('{group}.txt', group= metadata.group)

rule one:
    input:
        ids= lambda wc: metadata[metadata.group == int(wc.group)].id,
    output:
        out= '{group}.txt',
    shell:
        r"""
        echo {input.ids} > {output.out}
        """

You use a lambda function as input to query the dataframe metadata and return the id's corresponding to each {group}.您使用 lambda function 作为输入来查询 dataframe metadata并返回与每个{group}. Take care that the column group is assuming to be of type integer here.请注意,此处假设列group的类型为 integer。

If you're trying to use a 'function' in all the files in that directory you could use the follwing code:如果您尝试在该目录中的所有文件中使用“函数”,您可以使用以下代码:


import os

for path, dir, files in os.walk(os.getcwd()#Or your directory):
    for file in files:
       pass #Your code here!

path -> gives you the path for that file(does not include the file)路径-> 为您提供该文件的路径(不包括该文件)

dir -> gives you the directory of that file(not really usefull for you) dir -> 给你那个文件的目录(对你来说不是很有用)

files -> gives you a list of the files it the directory files -> 为您提供目录中的文件列表

note : The walk method is gonna 'walk' in every subfolder in your directory.注意:walk 方法将在目录中的每个子文件夹中“遍历”。 so if you have more directories in that folder have in mind it will include the files in there too因此,如果您在该文件夹中有更多目录,请记住它也会包含其中的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python 将一个文件夹中的多个 tsv 文件合并为一个 - Consolidate multiple tsv files from a folder into one using python 使用 Python 将具有相同文件名模式(但实际文件名不同)的多个 TSV 文件转换为 CSV - Converting multiple TSV files with same file name pattern (but different actual file names) into CSVs using Python 读取多个TSV文件并写入一个TSV文件Python - Read multiple TSV files and write to one TSV file Python 使用python重命名文件夹中的多个文件,其中包含文本文件中的新名称列表 - Renaming multiple files in a folder using python with list of new names from text file 如何在 Python 中打开 .tsv 文件? - How do I open a .tsv file in Python? 使用名称列表在python中打开多个文件 - using a list of names to open multiple files in python 如何从 python 的文件夹中打开多个 csv 文件? - How to open multiple csv files from a folder in python? 如何使用 tkinter 在 python 中 select 多个文件或整个文件夹(显示其中包含的所有文件的名称)? - How to select multiple files or an entire folder(display names of all the files it contains) in python using tkinter? 如何在python中打开txt文件并将内容放入tsv文件中 - How to open txt file and put content in tsv file in python 从python中的文件夹中读取多个.txt文件的名称 - Reading names of multiple .txt files from a folder in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM