双引号在Windows上使用os.system逃逸 [英] double quote escaping in os.system on windows
问题描述
我想逃离'',并在程序名和参数的所有其他野生字符,所以我尽量双引号他们。我可以在cmd.exe的做到这一点。
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']
但使用os.sytem有什么错如下code?
but what's wrong with the following code using 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,ABC识别为一个命令?但下面的例子不是
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']
谢谢!
推荐答案
与使用os.system尝试(蟒蛇test.py,ABC')
您也可以使用子的那种用途的模块,
You can also use subprocess module for that kind of purpose,
请看看<一个href=\"http://stackoverflow.com/questions/1885776/python-2-6-reading-data-from-a-windows-console-application-os-system/1885778#1885778\">this螺纹
更新:当我这样做,使用os.system('test.py,ABC')
,我得到了类似的错误,但不能在使用os.system('test.pyAbC')
,所以,我想先假设参数不应该是双引号括起来。
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
这篇关于双引号在Windows上使用os.system逃逸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!