Double quote in os.system on windows

I want to avoid "" and all other wild characters in the name and arguments of the program, so I'm trying to double 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 is wrong with the following code using os.sytem?

 cmd = '"test.py" "a" "b" "c"' print cmd os.system(cmd) 

its conclusion:

 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 entire string "test.py" "a" "b" "c" 'recognized by one command? But the following example:

 cmd = 'test.py abc' print cmd os.system(cmd) C:\bay\test\go>test2.py test.py abc hello ['C:\\bay\\test\\go\\test.py', 'a', 'b', 'c'] 

Thanks!

+3
source share
4 answers

Try with os.system('python "test.py" "a" "b" "c"')

You can also use the subprocess module for this purpose,

see this thread

UPDATE When I do this, os.system('"test.py" "a" "b" "c"') , I got similar errors, but not on os.system('test.py "a" "b" "c"') . Therefore, I like to assume that the first parameter should not be double quotes

+1
source

Furthing google comes to 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""' works!

+4
source

Actually, it just works as a design. You cannot use such an os.system. See this: http://mail.python.org/pipermail/python-bugs-list/2000-July/000946.html

+3
source

Replace the arguments in brackets, it works.

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

Source: https://habr.com/ru/post/972883/


All Articles