[英]double quote escaping in os.system on windows
我想在程序名稱和參數中轉義'“'和所有其他通配符,所以我嘗試將它們雙引號。可以在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']
謝謝!
谷歌進一步出現此頁面
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
嘗試使用os.system('python "test.py" "a" "b" "c"')
您也可以將子流程模塊用於此類目的,
請看看這個線程
更新 :當我這樣做時, os.system('"test.py" "a" "b" "c"')
,我遇到了類似的錯誤,但是沒有在os.system('test.py "a" "b" "c"')
,因此,我想假設第一個參數不應用雙引號引起來
將參數括在方括號中,即可正常工作。
CMD /k ("c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.