我想逃避程序名称和参数中的所有其他野生字符,所以我尝试把它们加双引号。我可以在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.
为什么整个string“test.py”“a”“b”“c”'被识别为单个命令? 但下面的例子不是:
cmd = 'test.py abc' print cmd os.system(cmd) C:\bay\test\go>test2.py test.py abc hello ['C:\\bay\\test\\go\\test.py', 'a', 'b', 'c']
谢谢!
尝试使用os.system('python "test.py" "a" "b" "c"')
你也可以使用子进程模块来达到这种目的,
请看看这个线程
更新 :当我这样做, os.system('"test.py" "a" "b" "c"')
,我有类似的错误,但没有在os.system('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""'
确实可行!
其实,它只是作为设计工作。 你不能像这样使用os.system。 看到这个: http : //mail.python.org/pipermail/python-bugs-list/2000-July/000946.html
括号括起来,它的作品。
CMD /k ("c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space")