开发者

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

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?

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" "开发者_JAVA百科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? 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

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!


Actually, it just work as design. You can NOT use os.system like that. See this: http://mail.python.org/pipermail/python-bugs-list/2000-July/000946.html


Try with 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


Enclose the arguments in brackets, it works.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜