[Tutor] How to pass a python variable to subprocess.call?
Steve Willoughby
steve at alchemy.com
Tue Oct 26 00:33:52 CEST 2010
On 25-Oct-10 15:19, Abhijeet Rastogi wrote:
> subprocess.call("ls -l "+mydir,shell=True)
> will do the job. In python, we can simply concatenate the strings like that.
>
> How do I replace /usr/local/bin in the subprocess call with the
> mydir variable?
It's often preferable (for reasons ranging from security to coding
style) to use the array call style for the subprocess methods, so
instead of a string which must then be interpreted by the shell (and
allowing for unintended interpretation of shell characters like quotes
and wildcards), you instead pass a list of values for the new process,
so the shell is never even involved:
subprocess.call(['ls', '-l', mydir])
More information about the Tutor
mailing list