Using "subprocess" without lists. . .?

M

Michael Williams

Hi All,

I've recently seen the "subprocess" module and am rather confused by
it's requirements. Is it not possible to execute an entire string
without having to break them up into a list of arguments? For
instance, I'd much rather do the following:


subprocess.call("ls -al | grep -i test")


.. . .than to have to:


list = ["ls", "-a", "-l" ] . . . . . . and so on and so forth.
subprocess.call(list. . .)


What is the best way to go about executing a massively complex single
line command?


Thanks,
Michael
 
S

Steven Bethard

Michael said:
Hi All,

I've recently seen the "subprocess" module and am rather confused by
it's requirements. Is it not possible to execute an entire string
without having to break them up into a list of arguments? For instance,
I'd much rather do the following:


subprocess.call("ls -al | grep -i test")


. . .than to have to:


list = ["ls", "-a", "-l" ] . . . . . . and so on and so forth.
subprocess.call(list. . .)


What is the best way to go about executing a massively complex single
line command?


You could always call "ls -al | grep -i test".split().

STeVe
 
P

Peter Otten

Michael said:
I've recently seen the "subprocess" module and am rather confused by
it's requirements. Is it not possible to execute an entire string
without having to break them up into a list of arguments? For
instance, I'd much rather do the following:


subprocess.call("ls -al | grep -i test")

Try

subprocess.call("ls -al | grep -i test", shell=True)
. . .than to have to:


list = ["ls", "-a", "-l" ] . . . . . . and so on and so forth.
subprocess.call(list. . .)

which avoids a lot of problems with shell quoting.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top