subprocess module

Peter Otten __peter__ at
Mon Jul 14 17:44:20 CEST 2008

John Mechaniks wrote:

> On Jul 14, 12:34 pm, Peter Otten <__pete... at> wrote:
>> John Mechaniks wrote:
>> > from subprocess import call
>> > call(['ls', '-l'])
>> > How do I get the result (not the exit status of the command) of "ls -
>> > l" into a variable?
>> output = subprocess.Popen(["ls", "-l"],
>> stdout=subprocess.PIPE)

> What difference does the following code makes? What are the advantages
> of the above method over this one?

> output = subprocess.Popen(['ls', '-l'],
> stdout=subprocess.PIPE).communicate()[0]

Hm, I chose it because it looks cleaner. Looking into the source
Popen.communicate() seems to do the following:

output = p._fo_read_no_intr(p.stdout)  

So there are two differences in this case

- communicate() waits for the subprocess to terminate.
- is retried if an EINTR occurs (Not sure when this would

> Also could someone show an example of using the optional input
> argument for communicate()

I didn't read it myself, but Doug Hellmann's articles are usually quite


More information about the Python-list mailing list