在你的程序内部,我假设你正在打开输入文件。所以当你分析参数时,为什么不允许通配符呢?
glob module就是你想要的。它将接受一个字符串并将其转换为一个列表文件路径,然后在您的程序中循环这些路径(或者使用multiprocess并行运行它们)。在
下面是它的工作原理。(我假设您使用argparse从命令行获取参数…)import glob
import argparse
import sys
from multiprocessing import Pool, cpu_count
def your_mappable_function(filename):
print filename.upper()
#parse the command line
parser = argparse.ArgumentParser(description='Example argparse')
parser.add_argument('-i', action="store", type=str)
parser.add_argument('-o', action="store", type=str)
args = parser.parse_args()
#glob to get the files from the wild card
input_file_list = glob.glob(args.i)
#use multiprocessing pool to process files in parallel
pool = Pool(cpu_count())
#results = map(your_mappable_function, input_file_list)
results = pool.map(your_mappable_function, input_file_list)
sys.exit(0)
如果您想要这些模块的教程,请查看Python Module of the Week。它涵盖了所有这些工具(以及更多)。您只需用一个文件名作为输入的函数包装器来替换“您的可映射的函数”,这样就可以开始了。这比bash脚本更快,因为在映射完整个列表之前,它不必启动和停止python进程。在
编辑:
当我回去确保代码运行时,我添加了一个函数包装器,以及一种在不使用进程池的情况下测试代码的方法。因为,每当你弄乱一个游泳池,它就会在一个错误上接管你的终端。在