【问题标题】:How to input all files within a directory with a certain ending? Python如何输入具有特定结尾的目录中的所有文件? Python
【发布时间】:2013-06-06 20:01:35
【问题描述】:

我有一个名为 Test '/Desktop/Test/' 的文件夹

我的文件夹中有几个文件(例如 1.fa,2.fa,3.fa,X.fa)

'/Desktop/Test/1.fa','/Desktop/Test/2.fa','/Desktop/Test/3.fa','/Desktop/Test/X.fa'

我正在尝试在我的函数中创建一个元素以打开目录中的每个文件 '/Desktop/Test/'.fa 结尾而不实际创建函数有 20 个左右的变量(我知道的唯一方法)

例子:

def simple(input):

    #input would be the directory '/Desktop/Test/'
    #for each .fa in the directory (eg. 1.fa, 2.fa, 3.fa, X.fa) i need it to create a list of all the strings within each file 
    #query=open(input,'r').read().split('\n') is what I would use in my simple codes that only have one input

如何输入一个以特定结尾(.fa)的目录中的所有文件?

【问题讨论】:

    标签: python file function input directory


    【解决方案1】:

    你可以这样做:

    import glob
    import os
    def simple(input)
        os.chdir(input)
        for file in glob.glob("*.fa"):
            with open(file, 'r+') as f:
                print f.readlines() #or do whatever
    

    【讨论】:

      猜你喜欢
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      相关资源
      最近更新 更多