python - 在 Windows 上的 os.system 中转义双引号

标签 python windows batch-file double-quotes

我想在程序名称和参数中转义 '"' 和所有其他的野生字符,所以我尝试用双引号将它们引起来。我可以在 cmd.exe 中执行此操作

C:\bay\test\go>"test.py" "a" "b"  "c"
hello
['C:\\bay\\test\\go\\test.py', 'a', 'b', 'c']

但是下面使用 os.sytem 的代码有什么问题?

cmd = '"test.py" "a" "b" "c"'
print cmd
os.system(cmd)

它的输出:

C:\bay\test\go>test2.py
"test.py" "a" "b" "c"
'test.py" "a" "b" "c' is not recognized as an internal or external command,
operable program or batch file.

为什么整个字符串 '"test.py""a""b""c"' 被识别为单个命令?但下面的例子不是:

cmd = 'test.py a b c'
print cmd
os.system(cmd)

C:\bay\test\go>test2.py
test.py a b c
hello
['C:\\bay\\test\\go\\test.py', 'a', 'b', 'c']

谢谢!

最佳答案

进一步谷歌来到这个页面

http://ss64.com/nt/syntax-esc.html

To launch a batch script which itself requires "quotes" 
CMD /k ""c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space"" 

cmd = '""test.py""a""b""c""' 确实有效!

关于python - 在 Windows 上的 os.system 中转义双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1912818/

相关文章:

python - Tkinter 中清爽的 BeautifulSoup

windows - 获取引导分区驱动器名称

batch-file - 如何在批处理脚本中拆分字符串

javascript - 使复选框功能的整行可单击,而无需停用复选框本身

python - 如何解决 ERROR Executor - Exception in task 0.0 in stage 20.0 (TID 20)?

python - Django POST 错误 : tuple has no attribute get, 尽管之前有类似的代码

windows - 如何在 Windows Phone 7 中绑定(bind)来自 xml 文件的动态多列表数据?

windows - 有没有办法列出Windows中的所有别名

c# - 在处理 HTTP 客户端时如何知道何时关闭套接字?

python - 如何从文本文件上下文菜单运行 python 脚本并将其与其他文本文件进行比较?