我是编程新手,我写了一个脚本从vcf文件中提取文本。我正在使用Linux虚拟机并运行Ubuntu。我已经通过命令行运行了这个脚本,方法是将我的目录更改为包含vcf文件的文件,然后输入python script.py。
我的脚本知道要处理哪个文件,因为脚本的开头是:
my_file = open("inputfile1.vcf", "r+")
outputfile = open("outputfile.txt", "w")脚本将我需要的信息放入列表中,然后将其写入输出文件。但是,我有许多输入文件(全部为.vcf),并希望将它们写入与输入(例如input_processed.txt)具有类似名称的不同输出文件。
是否需要运行shell脚本来遍历文件夹中的文件?如果是这样的话,我将如何更改python脚本以适应这种情况?就是把列表写到输出文件里?
发布于 2015-12-02 09:18:10
你不需要写shell脚本,也许这个问题对你有帮助?
发布于 2015-12-02 09:19:22
这取决于您如何实现迭代逻辑。
发布于 2015-12-02 09:29:01
我有一个我经常使用的脚本,包括使用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命令。
https://stackoverflow.com/questions/34038764
复制相似问题