Python 如何从命令行迭代和执行脚本?

Python 如何从命令行迭代和执行脚本?,python,subprocess,Python,Subprocess,我有一个从命令行运行的程序,它接受两个文件参数,如下所示: cmds = [path_to_program, arg1, arg2] subprocess.Popen(cmds) $my_程序 我想编写一个python脚本,可以使用不同的文件参数来运行这个程序的迭代。例如: $ my_program <file_1> <file_4> $ my_program <file_2> <file_4> $ my_program <file_3&g

我有一个从命令行运行的程序,它接受两个文件参数,如下所示:

cmds = [path_to_program, arg1, arg2]
subprocess.Popen(cmds)
$my_程序

我想编写一个python脚本,可以使用不同的文件参数来运行这个程序的迭代。例如:

$ my_program <file_1> <file_4>
$ my_program <file_2> <file_4>
$ my_program <file_3> <file_4>
...
$my\u程序
$my_程序
$my_程序
...
我试图使用python中的子流程模块来运行该程序的迭代。我似乎不知道如何通过不同的文件参数进行迭代,然后从命令行运行这些命令。谁能给我指出正确的方向,或者提供一些示例代码来解释如何做到这一点。提前感谢您的帮助

for fname in ('1', '2', '3'):
  do_something_with(['a', fname, 'b'])

根据需要替换
for
循环和函数调用中的序列。

看起来您想要的是执行bash脚本。在那里,您可以像在命令行中一样执行这些命令


您也可以进入主功能并执行它,但它没有那么灵活。

据我所知,您需要这样的功能:

cmds = [path_to_program, arg1, arg2]
subprocess.Popen(cmds)
然后,如果您想迭代许多,比如:

cmds = [('file1', 'file4'), ('file2', 'file4'), ('file3', 'file4')]
for arg1, arg2 in cmds:
    subprocess.Popen([path_to_program, arg1, arg2])