Sean Carolan | 26 Oct 00:01 2010
Picon

How to pass a python variable to subprocess.call?

I'm rewriting a bunch of my bash scripts to get some python practice.
There are some instances where python doesn't have the built-in text
processing that I need, so I'm using subprocess.call.  How can I pass
a python variable to these subprocesses?  For example:

mydir = "/usr/local/bin"
subprocess.call("ls -l /usr/local/bin", shell=True)

How do I replace /usr/local/bin in the subprocess call with the mydir variable?
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Abhijeet Rastogi | 26 Oct 00:19 2010
Picon

Re: How to pass a python variable to subprocess.call?


On Tue, Oct 26, 2010 at 3:31 AM, Sean Carolan <scarolan <at> gmail.com> wrote:
I'm rewriting a bunch of my bash scripts to get some python practice.
There are some instances where python doesn't have the built-in text
processing that I need, so I'm using subprocess.call.  How can I pass
a python variable to these subprocesses?  For example:

mydir = "/usr/local/bin"
subprocess.call("ls -l /usr/local/bin", shell=True)

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?
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



--
Abhijeet Rastogi (shadyabhi)
http://www.google.com/profiles/abhijeet.1989
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Steve Willoughby | 26 Oct 00:33 2010

Re: How to pass a python variable to subprocess.call?

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

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Bill Campbell | 26 Oct 00:08 2010
Picon

Re: How to pass a python variable to subprocess.call?

On Mon, Oct 25, 2010, Sean Carolan wrote:
>I'm rewriting a bunch of my bash scripts to get some python practice.
>There are some instances where python doesn't have the built-in text
>processing that I need, so I'm using subprocess.call.  How can I pass
>a python variable to these subprocesses?  For example:
>
>mydir = "/usr/local/bin"
>subprocess.call("ls -l /usr/local/bin", shell=True)
>
>How do I replace /usr/local/bin in the subprocess call with the mydir variable?

subprocess.call("ls -l '%s'" % mydir, shell=True)

Bill
--

-- 
INTERNET:   bill <at> celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:          (206) 236-1676  Mercer Island, WA 98040-0820
Fax:            (206) 232-9186  Skype: jwccsllc (206) 855-5792

Whatever the State saith is a lie; whatever it hath is a theft.
    -- Nietzsche
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Gmane