3

我想在程序名称和参数中转义 '"' 和所有其他通配符,所以我尝试将它们双引号。我可以在 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.

为什么整个字符串 '"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']

谢谢!

4

4 回答 4

5

进一步的谷歌来这个页面

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""'确实有效!

于 2009-12-16T08:04:58.510 回答
3

实际上,它只是作为设计工作。你不能像那样使用 os.system 。看到这个: http: //mail.python.org/pipermail/python-bugs-list/2000-July/000946.html

于 2009-12-16T07:47:41.603 回答
1

尝试os.system('python "test.py" "a" "b" "c"')

你也可以使用subprocess模块来达到这种目的,

请看看这个线程

更新:当我这样做时os.system('"test.py" "a" "b" "c"'),我得到了类似的错误,但不是os.system('test.py "a" "b" "c"'),所以,我想假设第一个参数不应该被双引号

于 2009-12-16T07:03:11.503 回答
0

将参数括在括号中,它可以工作。

CMD /k ("c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space")
于 2013-05-30T05:37:12.107 回答