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

发布于 2024-08-14 17:17:02 字数 735 浏览 7 评论 0原文

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

谢谢!

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" "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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

两个我 2024-08-21 17:17:02

进一步谷歌来到这个页面

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

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!

海拔太高太耀眼 2024-08-21 17:17:02

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

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

花开雨落又逢春i 2024-08-21 17:17:02

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

您也可以使用 subprocess 模块来实现这种目的,

请看一下 这个线程

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

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

萌面超妹 2024-08-21 17:17:02

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

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

Enclose the arguments in brackets, it works.

CMD /k ("c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文