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

2024-10-04 01:30:35 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我现有的(非功能性的)代码。你知道吗

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”的长度不总是固定的,如何正确地传递其内容?这是最简单的例子,但实际上我有类似的代码调用几个不同的命令。你知道吗


Tags: path代码sourceconvertosdefcalloutfile
1条回答
网友
1楼 · 发布于 2024-10-04 01:30:35

以平面列表或元组形式传递命令:

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是不允许的。你知道吗

相关问题 更多 >