简体   繁体   English

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

[英]double quote escaping in os.system on windows

I want to escape '"' and all other wild chars in program name and arguments, so I try to double quote them. and I can do this in cmd.exe 我想在程序名称和参数中转义'“'和所有其他通配符,所以我尝试将它们双引号。可以在cmd.exe中执行此操作

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

but what's wrong with the following code using os.sytem? 但是以下使用os.sytem的代码怎么了?

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

its output: 其输出:

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.

Why is the whole string '"test.py" "a" "b" "c"' recognized as a single command? 为什么整个字符串'“ test.py”“ a”“ b”“ c”'被识别为单个命令? But the following example isn't: 但是以下示例不是:

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']

Thanks! 谢谢!

Furthing google comes this page 谷歌进一步出现此页面

http://ss64.com/nt/syntax-esc.html 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""' does work! cmd = '""test.py" "a" "b" "c""'有效!

Actually, it just work as design. 实际上,它只是作为设计工作。 You can NOT use os.system like that. 您不能像这样使用os.system。 See this: http://mail.python.org/pipermail/python-bugs-list/2000-July/000946.html 看到这个: http : //mail.python.org/pipermail/python-bugs-list/2000-July/000946.html

Try with os.system('python "test.py" "a" "b" "c"') 尝试使用os.system('python "test.py" "a" "b" "c"')

You can also use subprocess module for that kind of purpose, 您也可以将子流程模块用于此类目的,

please take a look this thread 请看看这个线程

UPDATE :When I do, os.system('"test.py" "a" "b" "c"') , I got similar errors, but not on os.system('test.py "a" "b" "c"') , So, I like to assume that first parameter should not be double-quoted 更新 :当我这样做时, os.system('"test.py" "a" "b" "c"') ,我遇到了类似的错误,但是没有在os.system('test.py "a" "b" "c"') ,因此,我想假设第一个参数不应用双引号引起来

将参数括在方括号中,即可正常工作。

CMD /k ("c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM