为什么Python Popen会忽略字符’^’ – 如何在Popen Windows中转义’^’字符?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了为什么Python Popen会忽略字符’^’ – 如何在Popen Windows中转义’^’字符?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1610字,纯文字阅读大概需要3分钟。
内容图文
![为什么Python Popen会忽略字符’^’ – 如何在Popen Windows中转义’^’字符?](/upload/InfoBanner/zyjiaocheng/952/77a78700251c4a7a844e1d72df5fe530.jpg)
我准备了一些代码来执行这样的命令行:
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"
这可以从命令行(与上面相同的命令)工作正常,但352×352 ^是352×352 ^而不是352×352:
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()
解决方法:
Why Python cuts ‘^’ character and how to avoid it?
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)'] +
['^', '<-- see, it is still here'])
后一个命令使用遵循Parsing C Command-Line Arguments规则的subprocess.list2cmdline()将列表转换为命令字符串 – 它对^没有影响.
^ is not special for CreateProcess(). ^ is special if you use shell=True (when cmd.exe is run).
它包括^本身.
内容总结
以上是互联网集市为您收集整理的为什么Python Popen会忽略字符’^’ – 如何在Popen Windows中转义’^’字符?全部内容,希望文章能够帮你解决为什么Python Popen会忽略字符’^’ – 如何在Popen Windows中转义’^’字符?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。