Doble cita escapar en OS.System en Windows
-
19-09-2019 - |
Pregunta
Quiero escapar de '"' y todos los otros caracteres salvajes en el nombre y argumentos del programa, así que trato de citarlos. Y puedo hacerlo en cmd.exe
C:\bay\test\go>"test.py" "a" "b" "c"
hello
['C:\\bay\\test\\go\\test.py', 'a', 'b', 'c']
Pero, ¿qué pasa con el siguiente código usando OS.Sytem?
cmd = '"test.py" "a" "b" "c"'
print cmd
os.system(cmd)
su salida:
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.
¿Por qué la cadena completa "test.py" "a" "b" "c" 'se reconoce como un solo comando? Pero el siguiente ejemplo no es:
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']
¡Gracias!
Solución
Intentar os.system('python "test.py" "a" "b" "c"')
También puedes usar subproceso módulo para ese tipo de propósito,
por favor echa un vistazo este hilo
ACTUALIZAR:Cuando lo hago, os.system('"test.py" "a" "b" "c"')
, Tengo errores similares, pero no en os.system('test.py "a" "b" "c"')
, Entonces, me gusta suponer que el primer parámetro no debe ser doble
Otros consejos
Furthing Google llega esta página
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""'
¡funciona!
En realidad, solo funciona como diseño. No puede usar OS.System así. Mira esto:http://mail.python.org/pipermail/python-bugs-list/2000- july/000946.html
Adjunta los argumentos entre paréntesis, funciona.
CMD /k ("c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space")