jai*_*hen 3 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']
Run Code Online (Sandbox Code Playgroud)
但是以下使用 os.sytem 的代码有什么问题?
cmd = '"test.py" "a" "b" "c"'
print cmd
os.system(cmd)
Run Code Online (Sandbox Code Playgroud)
它的输出:
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.
Run Code Online (Sandbox Code Playgroud)
为什么整个字符串 '"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']
Run Code Online (Sandbox Code Playgroud)
谢谢!
进一步谷歌来到这个页面
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""
Run Code Online (Sandbox Code Playgroud)
cmd = '""test.py" "a" "b" "c""'
确实有效!
归档时间: |
|
查看次数: |
6799 次 |
最近记录: |