subprocess.Popen - file like object from stdout=PIPE

  • Thread starter Helmut Jarausch
  • Start date
H

Helmut Jarausch

Hi,

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

for line in EQ_OUT :
...

I could use StringIO.StringIO applied to EQ_output
but this reads all of the command's output into a big
string first.

On Unix/Linux a pipe is a file-like object after all,
so how to get hold of it.


Many thanks for a hint,
Helmut.

--
Helmut Jarausch

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

Chris Rebert

Hi,

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

for line in EQ_OUT :
...

Is there some reason that:

for line in EQ_OUT.splitlines():
#...

Does not meet your needs?

Cheers,
Chris
 
H

Helmut Jarausch

Chris said:
Hi,

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

for line in EQ_OUT :
...

Is there some reason that:

for line in EQ_OUT.splitlines():
#...

Does not meet your needs?

It works, but it still reads the complete output of the
command executed by Popen at once.

Helmut.


--
Helmut Jarausch

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

Clovis Fabricio

2009/2/4 Helmut Jarausch said:
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)
 
H

Helmut Jarausch

Clovis said:
2009/2/4 Helmut Jarausch said:
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
 
H

Helmut Jarausch

Clovis said:
2009/2/4 Helmut Jarausch said:
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
 
H

Helmut Jarausch

Clovis said:
That would be a documentation bug.
Fortunately it is not true. Here is it in the documentation:

http://docs.python.org/library/subprocess.html#subprocess.Popen.stdout

Thanks!

For people like me which don't always read until the end of a document
it would be nice if just after the paragraph

stdin, stdout and stderr specify the executed programs’ standard input, standard output and standard error file handles,
respectively. Valid values are PIPE, an existing file descriptor (a positive integer), an existing file object, and
None. PIPE indicates that a new pipe to the child should be created. With None, no redirection will occur; the child’s
file handles will be inherited from the parent. Additionally, stderr can be STDOUT, which indicates that the stderr data
from the applications should be captured into the same file handle as for stdout.

there would some lines like

These pipes are exposed as file-like objects which can be accessed via the
stdin, stdout or stderr (resp.) attributes of an object of class subprocess.Popen .

Please be indulgent to people like me.

Helmut.


--
Helmut Jarausch

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

Aahz

These pipes are exposed as file-like objects which can be accessed via
the stdin, stdout or stderr (resp.) attributes of an object of class
subprocess.Popen .

Please file a doc request at bugs.python.org
 

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,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top