subprocess.Popen - file like object from stdout=PIPE

Helmut Jarausch jarausch at skynet.be
Wed Feb 4 07:13:03 EST 2009


Clovis Fabricio wrote:
> 2009/2/4 Helmut Jarausch <jarausch at skynet.be>:
>> using e.g.
>> import subprocess
>> Package='app-arch/lzma-utils'
>> EQ=subprocess.Popen(['/usr/bin/equery','depends',Package],stdout=subprocess.PIPE)
>> EQ_output= EQ.communicate()[0]
>> EQ_output is a string containing multiple lines.
>> I'd prefer a file-like object, e.g. EQ_OUT
>> so that I can loop over the lines of it like
> 
> EQ.stdout is the filelike object you're looking for.
> communicate() grabs entire output at once so don't use it.
> 
> import subprocess
> Package = 'app-arch/lzma-utils'
> EQ = subprocess.Popen(['/usr/bin/equery', 'depends', Package],
> stdout=subprocess.PIPE)
> for line in EQ.stdout:
>     do_stuff_with(line)

Thanks a lot, I haven't found that in the official documentation.
Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany



More information about the Python-list mailing list