相关疑难解决方法(0)

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

我想在程序名称和参数中转义 '"' 和所有其他通配符,所以我尝试双引号它们。我可以在 cmd.exe 中执行此操作

C:\bay\test\go>"test.py" "a" "b"  "c"
hello
['C:\\bay\\test\\go\\test.py', 'a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)

但是以下使用 os.sytem 的代码有什么问题?

cmd = '"test.py" "a" "b" "c"'
print cmd
os.system(cmd)
Run Code Online (Sandbox Code Playgroud)

它的输出:

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.
Run Code Online (Sandbox Code Playgroud)

为什么整个字符串 '"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']
Run Code Online (Sandbox Code Playgroud)

谢谢!

python windows batch-file double-quotes

3
推荐指数
1
解决办法
6799
查看次数

标签 统计

batch-file ×1

double-quotes ×1

python ×1

windows ×1