Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > subprocess.Popen - file like object from stdout=PIPE

Reply
Thread Tools

subprocess.Popen - file like object from stdout=PIPE

 
 
Helmut Jarausch
Guest
Posts: n/a
 
      02-04-2009
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
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      02-04-2009
On Wed, Feb 4, 2009 at 1:22 AM, Helmut Jarausch <(E-Mail Removed)> wrote:
> 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

--
Follow the path of the Iguana...
http://rebertia.com
 
Reply With Quote
 
 
 
 
Helmut Jarausch
Guest
Posts: n/a
 
      02-04-2009
Chris Rebert wrote:
> On Wed, Feb 4, 2009 at 1:22 AM, Helmut Jarausch <(E-Mail Removed)> wrote:
>> 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
 
Reply With Quote
 
Clovis Fabricio
Guest
Posts: n/a
 
      02-04-2009
2009/2/4 Helmut Jarausch <(E-Mail Removed)>:
> 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)
 
Reply With Quote
 
Helmut Jarausch
Guest
Posts: n/a
 
      02-04-2009
Clovis Fabricio wrote:
> 2009/2/4 Helmut Jarausch <(E-Mail Removed)>:
>> 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
 
Reply With Quote
 
Helmut Jarausch
Guest
Posts: n/a
 
      02-04-2009
Clovis Fabricio wrote:
> 2009/2/4 Helmut Jarausch <(E-Mail Removed)>:
>> 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
 
Reply With Quote
 
Clovis Fabricio
Guest
Posts: n/a
 
      02-04-2009
2009/2/4 Helmut Jarausch <(E-Mail Removed)>:
>> EQ.stdout is the filelike object you're looking for.
>> communicate() grabs entire output at once so don't use it.

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


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

http://docs.python.org/library/subpr...s.Popen.stdout
 
Reply With Quote
 
Helmut Jarausch
Guest
Posts: n/a
 
      02-04-2009
Clovis Fabricio wrote:
> 2009/2/4 Helmut Jarausch <(E-Mail Removed)>:
>>> EQ.stdout is the filelike object you're looking for.
>>> communicate() grabs entire output at once so don't use it.

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

>
> That would be a documentation bug.
> Fortunately it is not true. Here is it in the documentation:
>
> http://docs.python.org/library/subpr...s.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
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      02-10-2009
In article <49899185$0$2861$(E-Mail Removed)>,
Helmut Jarausch <(E-Mail Removed)> wrote:
>
>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
--
Aahz ((E-Mail Removed)) <*> http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would destroy civilization.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When file-like objects aren't file-like enough for Windows William McBrine Python 1 03-16-2008 06:42 PM
Making a file-like object for manipulating a large file Sean Davis Python 2 08-26-2007 09:55 AM
concatenate file-like objects -> file-like object kgk Python 1 07-11-2007 06:17 AM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments