gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:42 24 4
gpt4 key购买 nike

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

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])

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

最佳答案

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

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 是不允许的。

关于python - 将未知长度的选项传递给子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25214858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com