问题描述
我准备了一些代码来执行这样的命令行:
I prepared some code to execute such command line:
c:cygwininconvert "c:
ootdropboxwww iffphotosarchitecturecalendar-bwl-projektwl01.tif" -thumbnail 352x352^ -format jpg -filter Catrom -unsharp 0x1 "c:
ootdropboxwww iff humbnailsarchitecturecalendar-bwl-projekt humbnailwl01.jpg"
这在命令行中可以正常工作(与上面的命令相同),但 352x352^ 是 352x352^ 而不是 352x352:
This works fine from command line (same command as above) but 352x352^ is 352x352^ not 352x352:
c:cygwininconvert "c:
ootdropboxwww iffphotosarchitecturecalendar-bwl-projektwl01.tif" -thumbnail 352x352^ -format jpg -filter Catrom -unsharp 0x1 "c:
ootdropboxwww iff humbnailsarchitecturecalendar-bwl-projekt humbnailwl01.jpg"
如果从 python 运行此代码 - 字符 '^' 被忽略并且调整大小的图像的大小为 '%sx%s' 传递而不是 %sx%s^ - 为什么 Python 会剪切 '^' 字符以及如何避免它?:
If run this code from python - character '^' is ignored and resized image has size as '%sx%s' is passed not %sx%s^ - Why Python cuts '^' character and how to avoid it?:
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 会削减 '^' 字符以及如何避免?
Why Python cuts '^' character and how to avoid it?
Python 不会剪切 ^
字符.Popen()
将字符串 (resize_command
) 按原样传递给 CreateProcess()
Windows API 调用.
Python does not cut ^
character. Popen()
passes the string (resize_command
) to CreateProcess()
Windows API call as is.
很容易测试:
#!/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()">Parsing C Command-Line Arguments 规则将列表转换为命令字符串 -- 它对 ^
没有影响.
The latter command uses subprocess.list2cmdline()
that follows Parsing C Command-Line Arguments rules to convert the list into the command string -- it has not effect on ^
.
^
对于 CreateProcess 来说并不特殊()代码>.
^
如果你使用 shell=True
是特殊的(当 cmd.exe
运行时).
^
is not special for CreateProcess()
. ^
is special if you use shell=True
(when cmd.exe
is run).
当且仅当生成的命令行将由 cmd 解释,在每个 shell 元字符(或每个字符)前加上 ^
字符.它包括 ^
本身.
if and only if the command line produced will be interpreted by cmd, prefix each shell metacharacter (or each character) with a ^
character. It includes ^
itself.
这篇关于为什么字符'^'被Python Popen忽略 - 如何在Popen Windows中转义'^'字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!