为什么字符“^”被Python Popen忽略-如何在Popen窗口中转义“^”字符?

为什么字符“^”被Python Popen忽略-如何在Popen窗口中转义“^”字符?,python,windows,python-2.7,imagemagick,subprocess,Python,Windows,Python 2.7,Imagemagick,Subprocess,我准备了一些代码来执行这样的命令行: c:\cygwin\bin\convert "c:\root\dropbox\www\tiff\photos\architecture\calendar-bwl-projekt\bwl01.tif" -thumbnail 352x352^ -format jpg -filter Catrom -unsharp 0x1 "c:\root\dropbox\www\tiff\thumbnails\architecture\calendar-bwl-projekt\

我准备了一些代码来执行这样的命令行:

c:\cygwin\bin\convert "c:\root\dropbox\www\tiff\photos\architecture\calendar-bwl-projekt\bwl01.tif" -thumbnail 352x352^ -format jpg -filter Catrom -unsharp 0x1 "c:\root\dropbox\www\tiff\thumbnails\architecture\calendar-bwl-projekt\thumbnail\bwl01.jpg"
这在命令行中可以正常工作,与上面的命令相同,但352x352^是352x352^而不是352x352:

c:\cygwin\bin\convert "c:\root\dropbox\www\tiff\photos\architecture\calendar-bwl-projekt\bwl01.tif" -thumbnail 352x352^ -format jpg -filter Catrom -unsharp 0x1 "c:\root\dropbox\www\tiff\thumbnails\architecture\calendar-bwl-projekt\thumbnail\bwl01.jpg"
如果从python运行此代码-忽略字符“^”,并且调整大小后的图像大小为“%sx%s”而不是%sx%s^-python为什么要剪切“^”字符以及如何避免它

Python为什么要剪切“^”字符以及如何避免它

Python不剪切^character。Popen将字符串resize_命令按原样传递给CreateProcess Windows API调用

很容易测试:

#!/usr/bin/env python
import sys
import subprocess

subprocess.check_call([sys.executable, '-c', 'import sys; print(sys.argv)'] +
                      ['^', '<-- see, it is still here'])
后一个命令使用subprocess.list2cmdline(遵循规则)将列表转换为命令字符串-它对^没有影响


。它包括^1本身。

尝试将列表而不是字符串传递给Popen。这有时会有帮助。@Kevin不管看到什么模块代码,下面的列表都会被加入。无关:Popencmd.wait是subprocess.callcmd。如果我没有弄错,问题实际上与问题中指定的相反-在命令行键入命令时,^会被删除,但在使用Popen时会保留。@Markransem:是。^转义命令行上的下一个字符,但如果在没有shell=True的情况下使用Popen,则没有特殊意义。我猜可能是OP没有提供实际代码,即使用了shell=True,或者convert命令损坏了参数本身。我被添加了shell=True tp remove问题-现在我更好地理解了根本原因-非常好的解释。@eryksun:很清楚,答案不建议shell=True。它只是说,如果没有shell=True,cmd.exe不会解释,^不是特殊的,即,^按原样传递给命令。
#!/usr/bin/env python
import sys
import subprocess

subprocess.check_call([sys.executable, '-c', 'import sys; print(sys.argv)'] +
                      ['^', '<-- see, it is still here'])