为什么Python Popen会忽略字符'^' - 如何在Popen Windows中转义'^'字符? [英] Why character '^' is ignored byt Python Popen - how to escape '^' character in Popen Windows?

查看:431
本文介绍了为什么Python Popen会忽略字符'^' - 如何在Popen Windows中转义'^'字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  c:\ cygwin \ bin\convertc :\root\dropbox\www\tiff\photos\architecture\calendar-BWL-projekt\bwl01.tif -thumbnail 352x352 ^ -format JPG型滤波器Catrom -unsharp为0x1 C:\ root\dropbox\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 

这可以从命令行运行(与上面相同的命令),但352x352 ^是352x352 ^不是352x352:

  c:\ cygwin \ bin\convertc:\root \ dropbox \\\\tiff\photos \architecture \ calendar-bwl-projekt \ bwl01.tif -thumbnail 352x352 ^ -format JPG型滤波器Catrom -unsharp为0x1 C:\root\dropbox\www\tiff\thumbnails\architecture\calendar-BWL-projekt\thumbnail\bwl01.jpg 

如果从py运行此代码thon - 字符'^'被忽略,调整大小的图像的大小为'%sx%s'传递而不是%sx%s ^ - 为什么Python剪切'^'字符以及如何避免它?

  def resize_image_to_jpg(input_file,output_file,size):
resize_command ='c:\\cygwin \ \\\\\转换%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()将字符串( resize_command )传递给 CreateProcess() Windows API调用原样。



很容易测试:

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

subprocess.check_call([sys.executable,' - c','import sys; print(sys.argv)'] +
['^','< - 看,它还在这里'])

后一个命令使用<$ href =http://msdn.microsoft.com/之后的 subprocess.list2cmdline() en-us / library / a1y7w461.aspxrel =nofollow>解析C命令行参数规则将列表转换为命令字符串 - 它对 ^ <没有影响/ code>。



^ 对于 CreateProcess()并不特殊。 ^ 如果您使用 shell = True (当 cmd.exe 正在运行)



当且仅当生成的命令行将由cmd解释时,为每个shell元字符添加前缀(或每个字符)带有 ^ 字符。它包括 ^ 本身。


I prepared some code to execute such command line:

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"

This works fine from command line (same command as above) but 352x352^ is 352x352^ not 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"

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

解决方案

Why Python cuts '^' character and how to avoid it?

Python does not cut ^ character. Popen() passes the string (resize_command) to CreateProcess() Windows API call as is.

It is easy to test:

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

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

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 ^.

^ is not special for CreateProcess(). ^ is special if you use shell=True (when cmd.exe is run).

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中转义'^'字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆