subprocess module

John Mechaniks sn.oo.py.9027 at gmail.com
Mon Jul 14 18:41:15 CEST 2008


On Jul 14, 7:44 pm, Peter Otten <__pete... at web.de> wrote:
> John Mechaniks wrote:
> > On Jul 14, 12:34 pm, Peter Otten <__pete... at web.de> 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).stdout.read()
> > 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)  
> p.wait()            
>
> So there are two differences in this case
>
> - communicate() waits for the subprocess to terminate.
> - stdout.read() is retried if an EINTR occurs (Not sure when this would
> happen).
>
> > Also could someone show an example of using the optional input
> > argument for communicate()
>
> http://blog.doughellmann.com/2007/07/pymotw-subprocess.html
>
> I didn't read it myself, but Doug Hellmann's articles are usually quite
> good.
>
> Peter

Thanks Peter



More information about the Python-list mailing list