By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 1,976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

Using "subprocess" without lists. . .?

P: n/a
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
May 13 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Michael Williams wrote:
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
May 13 '07 #2

P: n/a
Michael Williams wrote:
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
May 13 '07 #3

P: n/a
Steven Bethard wrote:
You could always call "ls -al | grep -i test".split().
Or better, shlex.split().

Peter
May 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.