Doppelte Zitat entkommen in os.System unter Windows
-
19-09-2019 - |
Frage
Ich möchte "und allen anderen wilden Chars im Programmnamen und in den Argumenten entkommen, also versuche ich, sie zu verdoppeln. Und ich kann dies in CMD.exe tun
C:\bay\test\go>"test.py" "a" "b" "c"
hello
['C:\\bay\\test\\go\\test.py', 'a', 'b', 'c']
Aber was ist los mit dem folgenden Code mit OS.Sytem?
cmd = '"test.py" "a" "b" "c"'
print cmd
os.system(cmd)
seine Ausgabe:
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.
Warum wird der gesamte String '"test.py" "A" B "" C "' als einzelner Befehl erkannt? Das folgende Beispiel ist jedoch nicht:
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']
Vielen Dank!
Lösung
Versuche es mit os.system('python "test.py" "a" "b" "c"')
Sie können auch verwenden Subprozess Modul für diese Art von Zweck,
Bitte schau es dir an Dieser Thread
AKTUALISIEREN: Wenn ich das tue, os.system('"test.py" "a" "b" "c"')
, Ich habe ähnliche Fehler, aber nicht auf os.system('test.py "a" "b" "c"')
, Ich gehe also gerne davon aus, dass der erste Parameter nicht doppelt zitiert werden sollte
Andere Tipps
Feder Google kommt diese Seite
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""'
funktioniert!
Eigentlich funktioniert es einfach als Design. Sie können OS.System nicht so verwenden. Sieh dir das an:http://mail.python.org/pipermail/python-bugs-list/2000-july/000946.html
Schließen Sie die Argumente in Klammern ein, es funktioniert.
CMD /k ("c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space")