[Tutor] string conversion according to the terminal

Peter Otten __peter__ at web.de
Thu Aug 12 15:28:03 CEST 2010


ANKUR AGGARWAL wrote:

> Hey- suppose we have a file name-"my file number"
> if we want to execute it into the terminal it would be like - my\ file\
> number
> 
> so wondering is there any one in the python that change the enter string
> into the terminal string one-
> like if user enter the file name with path- "my file number". i want to
> automatically convert it into "my\ file\ number"

If you want this to prepare an os.system() call you should instead use 
subprocess.call() with a list containing the command and a filename that is 
not escaped.

>>> import subprocess, os

Bad:

>>> os.system(r"ls -l my\ file\ number")
-rw-r--r-- 1 petto petto 0 2010-08-12 15:20 my file number
0

Good:

>>> subprocess.call(["ls", "-l", "my file number"])
-rw-r--r-- 1 petto petto 0 2010-08-12 15:20 my file number
0

Peter



More information about the Tutor mailing list