[Tutor] Turning multiple Popen calls into a function

jay titleistfour at gmail.com
Thu Nov 1 18:49:55 CET 2007


If I have multiple Popen calls I need to make, how can I turn these into a

from subprocess import Popen, PIPE

p1 = Popen(['ls', '-l', '-a', '/etc'],stdout=PIPE)
p2 = Popen(['grep', 'hosts'], stdin=p1.stdout, stdout=PIPE)
p3 = Popen(['awk', '{print $1}'], stdin=p2.stdout, stdout=PIPE)
output = p3.communicate()[0]

p1 = Popen(['ps', '-ef'], stdout=PIPE)
p2 = Popen(['grep', '-v', '2348'], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]

I would be sending an arbitrary number of PIPES with each function call.

I'm a little stumped as to how to handle the variables.  If I have an
arbitrary number of PIPES, how do I declare my variables (p1, p2, p3,
etc...) ahead of time in the function??

Thanks for any suggestions!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071101/e53c0e8a/attachment.htm 

More information about the Tutor mailing list