Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.python

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-04-2009, 08:22 AM
Helmut Jarausch
Guest
 
Posts: n/a
Default subprocess.Popen - file like object from stdout=PIPE

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
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 02-04-2009, 08:43 AM
Chris Rebert
Guest
 
Posts: n/a
Default Re: subprocess.Popen - file like object from stdout=PIPE

On Wed, Feb 4, 2009 at 1:22 AM, Helmut Jarausch <jarausch@skynet.be> 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
  #3 (permalink)  
Old 02-04-2009, 09:48 AM
Helmut Jarausch
Guest
 
Posts: n/a
Default Re: subprocess.Popen - file like object from stdout=PIPE

Chris Rebert wrote:
> On Wed, Feb 4, 2009 at 1:22 AM, Helmut Jarausch <jarausch@skynet.be> 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
  #4 (permalink)  
Old 02-04-2009, 11:06 AM
Clovis Fabricio
Guest
 
Posts: n/a
Default Re: subprocess.Popen - file like object from stdout=PIPE

2009/2/4 Helmut Jarausch <jarausch@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)
Reply With Quote
  #5 (permalink)  
Old 02-04-2009, 11:13 AM
Helmut Jarausch
Guest
 
Posts: n/a
Default Re: subprocess.Popen - file like object from stdout=PIPE

Clovis Fabricio wrote:
> 2009/2/4 Helmut Jarausch <jarausch@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
Reply With Quote
  #6 (permalink)  
Old 02-04-2009, 11:13 AM
Helmut Jarausch
Guest
 
Posts: n/a
Default Re: subprocess.Popen - file like object from stdout=PIPE

Clovis Fabricio wrote:
> 2009/2/4 Helmut Jarausch <jarausch@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
Reply With Quote
  #7 (permalink)  
Old 02-04-2009, 11:19 AM
Clovis Fabricio
Guest
 
Posts: n/a
Default Re: subprocess.Popen - file like object from stdout=PIPE

2009/2/4 Helmut Jarausch <jarausch@skynet.be>:
>> 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
  #8 (permalink)  
Old 02-04-2009, 12:00 PM
Helmut Jarausch
Guest
 
Posts: n/a
Default Re: subprocess.Popen - file like object from stdout=PIPE

Clovis Fabricio wrote:
> 2009/2/4 Helmut Jarausch <jarausch@skynet.be>:
>>> 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
  #9 (permalink)  
Old 02-10-2009, 06:18 PM
Aahz
Guest
 
Posts: n/a
Default Re: subprocess.Popen - file like object from stdout=PIPE

In article <49899185$0$2861$ba620e4c@news.skynet.be>,
Helmut Jarausch <jarausch@skynet.be> 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 (aahz@pythoncraft.com) <*> 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
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Noobie here - EOF (hex 1A) Q. (long post) Duh_OZ Newsgroup comp.soft-sys.sas 0 03-11-2008 12:33 PM
Re: write to file without data step? data _null_; Newsgroup comp.soft-sys.sas 0 06-19-2007 06:38 PM
Code to automatically import sets from within access mdb tables Friar Broccoli Newsgroup comp.soft-sys.sas 1 11-17-2006 03:32 PM
Re: Hash object (was Re: Dynamically created dataset using hash Dorfman, Paul Newsgroup comp.soft-sys.sas 0 08-28-2006 06:40 AM
Re: PC SAS Transport file is bad Gerhard Hellriegel Newsgroup comp.soft-sys.sas 1 05-12-2006 10:10 AM



All times are GMT. The time now is 07:52 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.