views:

734

answers:

3

Hi, I want to escape '"' and all other wild chars in program name and arguments, so I try to double quote 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's wrong with the following code using os.sytem?

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

its output:

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 whole string '"test.py" "a" "b" "c"' recognized as a single command? But the following example isn't:

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']

Thanks!

+1  A: 

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

You can also use subprocess module for that kind of purpose,

please take a look this thread

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

S.Mark
Yes, it works with full python path.But what if the program is not a python script?Why does it consider them as a whole string when all parts wrapped with double quote? Still don't understand.
chenz
@Johannes Rössel, I tested in my windows actually, may I know what kind of errors you got?
S.Mark
@chenz, please take a look this thread, http://stackoverflow.com/questions/1885776/python-2-6-reading-data-from-a-windows-console-application-os-system/1885778#1885778
S.Mark
Sorry S.Mark. I didn't try it. I just extrapolated from the fact that apparently `system()` took the entire string as its command and try to run it but I couldn't confirm it upon reading the docs. Should have deleted that earlier.
Joey
subprocess can do some sort of this job, if properly escaped, but I wonder what's wrong with os.system?
chenz
Hi Johannes Rössel, no problem, actually, I am also didn't make sense that much, according to his output `('test.py" "a" "b" "c')`, I thought there is **double-quote "** after test.py, so I just regenerated like this, os.system('"test.py" "a" "b" "c"') I got similar error, so I assumed like first parameters should not be inside double-quotes
S.Mark
@S.Mark, yeah, normally people don't escape the first argument, which is actually the program you want to run, but if you have spaces in it, you got to find a way to escape.
chenz
Absolutely Correct chenz, So you should give it a try with subprocess module.
S.Mark
Mark as solved. better use subprocess.
chenz
+1  A: 

Actually, it just work as design. You can NOT use os.system like that. See this: http://mail.python.org/pipermail/python-bugs-list/2000-July/000946.html

Hey
A: 

Furthing google comes 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""' does work!

chenz