While it is handy to be able to execute commands pretty much as you would on the command line, you often need to pass variables from Python to the commands you are using. Let's say we wanted to re-write this function to use echo:
def print_string(string):
print string
You might do this:
def print_string(string):
subprocess.Popen('echo "%s"'%string, shell=True)
This will work fine for the string Hello World!:
>>> print_string('Hello world!')
Hello world!
But not for this:
>>> print_string('nasty " example')
/bin/sh: Syntax error: Unterminated quoted string
The command being executed is echo "nasty " example" and as you can see, there is a problem with the escaping.
One approach is to deal with the escaping in your code but this can be cumbersome because you have to deal with all the possible escape characters, spaces etc.
Python can handle it for you but you have to avoid using the shell. Let's look at this next.
Without the Shell
Now let's try the same thing without the shell:
def print_string(string):
subprocess.Popen(['echo', string], shell=False)
>>> print_string('Hello world!')
Hello world!
>>> print_string('nasty " example')
nasty " example
As you can see it works correctly with the correct escaping.
Note
You can actually specify a single string as the argument when shell=False but it must be the program itself and is no different from just specifying a list with one element for args. If you try to execute the same sort of command you would when shell=False you get an error:
>>> subprocess.Popen('echo "Hello world!"', shell=False)
Traceback (most recent call last):
File "
File "/usr/lib/python2.5/subprocess.py", line 594, in __init__
errread, errwrite)
File "/usr/lib/python2.5/subprocess.py", line 1147, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Since we are still passing it as a string, Python assumes the entire string is the name of the program to execute and there isn't a program called echo "Hello world!" so it fails. Instead you have to pass each argument separately.
Comments
Post a Comment
https://gengwg.blogspot.com/