【问题标题】:How can I run a python script on many files to get many output files?如何在多个文件上运行 python 脚本以获取多个输出文件?
【发布时间】:2016-03-06 11:01:44
【问题描述】:

我是编程新手,我编写了一个脚本来从 vcf 文件中提取文本。我正在使用 Linux 虚拟机并运行 Ubuntu。我已通过命令行运行此脚本,方法是将我的目录更改为包含 vcf 文件的文件,然后输入 python script.py

我的脚本知道要处理哪个文件,因为我的脚本的开头是:

my_file = open("inputfile1.vcf", "r+")
outputfile = open("outputfile.txt", "w")

脚本将我需要的信息放入一个列表中,然后我将其写入输出文件。但是,我有很多输入文件(全部为.vcf),并希望将它们写入与输入名称相似的不同输出文件(例如input_processed.txt)。

我是否需要运行 shell 脚本来遍历文件夹中的文件?如果是这样,我将如何更改 python 脚本以适应这一点?即将列表写入输出文件?

【问题讨论】:

  • 迭代输入文件名

标签: python linux bash shell python-2.7


【解决方案1】:

我会将它集成到 Python 脚本中,这样您也可以轻松地在其他平台上运行它,而且不会添加太多代码。

import glob
import os

# Find all files ending in 'vcf'
for vcf_filename in glob.glob('*.vcf'):
    vcf_file = open(vcf_filename, 'r+')

    # Similar name with a different extension
    output_filename = os.path.splitext(vcf_filename)[0] + '.txt'
    outputfile = open(output_filename, 'w')

    # Process the data
    ...

要将生成的文件输出到单独的目录中,我会:

import glob
import os

output_dir = 'processed'
os.makedirs(output_dir, exist_ok=True)

# Find all files ending in 'vcf'
for vcf_filename in glob.glob('*.vcf'):
    vcf_file = open(vcf_filename, 'r+')

    # Similar name with a different extension
    output_filename = os.path.splitext(vcf_filename)[0] + '.txt'
    outputfile = open(os.path.join(output_dir, output_filename), 'w')

    # Process the data
    ...

【讨论】:

  • 嗨!这非常有效!虽然原始文件是数字和文本,所以我使用“_”作为分隔符进行拆分,然后在末尾添加“_processed”。我现在唯一想知道的另一件事是如何将它们全部添加到我所在目录中的文件夹中?如,将所有文件添加到一个名为“已处理”的新文件夹中?
  • 我已经更新了答案以显示我将如何为排序脚本执行此操作。
【解决方案2】:

你不需要写shell脚本, 也许这个问题会对你有所帮助?

How to list all files of a directory?

【讨论】:

    【解决方案3】:

    这取决于你如何实现迭代逻辑。

    1. 如果你想在python中实现,就去做吧;

    2. 如果你想在shell脚本中实现它,只需将你的python脚本改为接受参数,然后使用shell脚本调用你合适的参数的python脚本。

    【讨论】:

      【解决方案4】:

      我有一个我经常使用的脚本,其中包括使用 PyQt5 弹出一个窗口,提示用户选择一个文件...然后它遍历目录以查找目录中的所有文件:

      pathname = first_fname[:(first_fname.rfind('/') + 1)] #figures out the pathname by finding the last '/'
      new_pathname = pathname + 'for release/' #makes a new pathname to be added to the names of new files so that they're put in another directory...but their names will be altered 
      
      file_list = [f for f in os.listdir(pathname) if f.lower().endswith('.xls') and not 'map' in f.lower() and not 'check' in f.lower()] #makes a list of the files in the directory that end in .xls and don't have key words in the names that would indicate they're not the kind of file I want
      

      您需要导入 os 才能使用 os.listdir 命令。

      【讨论】:

        【解决方案5】:

        您可以使用 listdir(您需要编写条件来过滤特定扩展名) 或 glob。我通常更喜欢 glob。例如

        import os
        import glob
        for file in glob.glob('*.py'):
            data = open(file, 'r+')
            output_name = os.path.splitext(file)[0]
            output = open(output_name+'.txt', 'w')
            output.write(data.read())
        

        此代码将从输入中读取内容并将其存储在输出文件中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-03
          • 1970-01-01
          • 2021-11-28
          • 1970-01-01
          • 2020-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多