[英]'^' is ignored by Python - how to escape '^' character in Popen Windows?
我准备了一些代码来执行这样的命令行:
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 会删除^
字符,我该如何避免呢?
def resize_image_to_jpg(input_file, output_file, size):
resize_command = 'c:\\cygwin\\bin\\convert "%s" -thumbnail %sx%s^ -format jpg -filter Catrom -unsharp 0x1 "%s"' \
% (input_file, size, size, output_file)
print resize_command
resize = subprocess.Popen(resize_command)
resize.wait()
为什么 Python 会削减 '^' 字符以及如何避免它?
Python 不会剪切^
字符。 Popen Popen()
按原样将字符串 ( resize_command
) 传递给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'])
后一个命令使用遵循解析 C 命令行参数规则的subprocess.list2cmdline()
将列表转换为命令字符串——它对^
没有影响。
^
对于CreateProcess()
并不特别。 如果您使用shell=True
(当cmd.exe
运行时) , ^
是特殊的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.