shell command needs whitespace characters escaped

Fredrik Lundh fredrik at pythonware.com
Fri Dec 8 11:58:01 EST 2006


metaperl wrote:

> I downloaded a file which has a space in the filename. I want to run a
> shell unzip on it, but it fails in my current code:
> 
>         syscmd = "cd %s ; unzip %s" % (self.storage.input,
> file.basename())
>         os.system(syscmd)
> 
> because no escaping was done.
> 
> Is there a more principled way to construct a shell command and execute
> it so that all necessary characters are escaped?

use subprocess.list2cmdline to create the command string (or better, 
subprocess.call).  see this FAQ entry for sample code:

http://effbot.org/pyfaq/why-can-t-raw-strings-r-strings-end-with-a-backslash.htm

</F>




More information about the Python-list mailing list