|
|||
|
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 |
|
|
||||
|
||||
|
|
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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) |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |