首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在许多文件上运行python脚本以获得许多输出文件?

问如何在许多文件上运行python脚本以获得许多输出文件?
EN

Stack Overflow用户
提问于 2015-12-02 09:11:16
回答 4查看 970关注 0票数 1

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

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

代码语言:javascript
复制
my_file = open("inputfile1.vcf", "r+")
outputfile = open("outputfile.txt", "w")

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

是否需要运行shell脚本来遍历文件夹中的文件?如果是这样的话,我将如何更改python脚本以适应这种情况?就是把列表写到输出文件里?

EN

回答 4

Stack Overflow用户

发布于 2015-12-02 09:18:10

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

How to list all files of a directory?

票数 0
EN

Stack Overflow用户

发布于 2015-12-02 09:19:22

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

  1. 如果您想在python中实现它,只需这样做;
  2. 如果希望在shell脚本中实现它,只需将python脚本更改为接受参数,然后使用shell脚本使用适当的参数调用python脚本。
票数 0
EN

Stack Overflow用户

发布于 2015-12-02 09:29:01

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

代码语言:javascript
复制
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命令。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34038764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档