子流程中如何不引用参数? [英] How not to quote argument in subprocess?
问题描述
我正在尝试使用subprocess.call从Python 2.7调用ImageMagick命令.我的问题是子进程中的参数解析器在每个参数周围都加上了双引号,而ImageMagick似乎在非文件参数周围使用了引号.
I'm trying to call an ImageMagick command from Python 2.7 using subprocess.call. My problem is that the argument parser in subprocess puts a double quotation mark around every argument, and ImageMagick seems to have a problem with quotes around non-file arguments.
我想要的是这样的东西
"imagemagick.exe" "im1.png" "im2.png" -alpha off ( ... ) -composite "im3.png"
到目前为止,除了用丑陋的 +" +
元素手动构造字符串并使用 shell = True 来手工构造字符串之外,到目前为止,我找不到用子进程来完成此操作的方法.代码>.
So far I couldn't find a way to do it with subprocess, other than manually constructing the string with ugly + " " +
elements and calling it with shell=True
.
这是我的代码:
args = [ imagemagick,
filename + "_b.bmp",
filename + "_w.bmp",
"-alpha off ( -clone 0,1 -compose difference -composite -negate ) ( -clone 0,2 +swap -compose divide -composite ) -delete 0,1 +swap -compose Copy_Opacity -composite",
filename + ".png" ]
subprocess.call( args )
有什么方法可以使用子进程调用没有双引号的正确命令吗?
Is there any way to call the correct command without double quotes using subprocess?
更新:插入了完整的命令行.我想把那部分放在一起,而不是"alpha",-off",……一一对应.
Update: inserted the full command line. I'd like to keep that part together, not as "alpha", "-off", ... one by one.
推荐答案
使用 subprocess
调用外部程序时, every 参数必须是 args的不同元素
.因此,尝试:
When calling external programs with subprocess
, every argument must be a different element of args
. So try:
args = [ imagemagick,
filename + "_b.bmp",
filename + "_w.bmp",
"-alpha", "off", "( ... )", "-composite",
filename + ".png" ]
我不确定(...)
代表什么,但是如果您在命令行中将未引号的空格放在其中,则它们应该是 args
也是如此.
I'm not sure what the ( ... )
represents, but if you put unquoted spaces in there on the command line, they should be separate elements in args
too.
这篇关于子流程中如何不引用参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!