将未知长度的选项传递给子进程

时间:2014-08-09 03:07:16

标签: python subprocess python-2.x

这是我现有的(非功能性)代码。

def call_GM(sourcefile):
    source = os.path.splitext(sourcefile)
    outfile = '"' + source[0] + '_straightened' + source[1] + '"'
    options = ('convert', '-auto-orient', sourcefile, outfile)
    command = 'gm'
    subprocess.call([command, options])

如何正确传递"选项"的内容?鉴于它的长度并不总是固定的?这是最简单的例子,但我实际上有类似的代码调用几个不同的命令。

1 个答案:

答案 0 :(得分:1)

将命令作为平面列表或元组传递:

def call_GM(sourcefile):
    source = os.path.splitext(sourcefile)
    outfile = '"' + source[0] + '_straightened' + source[1] + '"'
    options = ['convert', '-auto-orient', sourcefile, outfile]
    command = 'gm'
    subprocess.call([command] + options)

注意:将options修改为列表,因为不允许list + tuple