Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > output to console and to multiple files

Reply
Thread Tools

output to console and to multiple files

 
 
Bart Ogryczak
Guest
Posts: n/a
 
      02-16-2007
On Feb 14, 11:28 pm, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> Hello,
>
> I searched on Google and in this Google Group, but did not find any
> solution to my problem.
>
> I'm looking for a way to output stdout/stderr (from a subprocess or
> spawn) to screen and to at least two different files.
>
> eg.
> stdout/stderr -> screen
> stdout -> log.out
> stderr -> log.err
>
> and if possible
> stdout/stderr -> screen and log.txt
>
> 3 files from stdout/stderr


I'd derive a class from file, overwrite it's write() method to send a
copy to the log, and then assign sys.stdout = newFile(sys.stdout).
Same for stderr.





 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-16-2007
En Fri, 16 Feb 2007 14:04:33 -0300, Bart Ogryczak <(E-Mail Removed)>
escribió:

> On Feb 14, 11:28 pm, "(E-Mail Removed)" <(E-Mail Removed)>
> wrote:


>> I'm looking for a way to output stdout/stderr (from a subprocess or
>> spawn) to screen and to at least two different files.

>
> I'd derive a class from file, overwrite it's write() method to send a
> copy to the log, and then assign sys.stdout = newFile(sys.stdout).
> Same for stderr.


That's ok inside the same process, but the OP needs to use it "from a
subprocess or spawn".
You have to use something like tee, working with real file handles.

--
Gabriel Genellina

 
Reply With Quote
 
 
 
 
garrickp@gmail.com
Guest
Posts: n/a
 
      02-16-2007
On Feb 16, 3:28 pm, "Gabriel Genellina" <(E-Mail Removed)> wrote:

>
> That's ok inside the same process, but the OP needs to use it "from a
> subprocess or spawn".
> You have to use something like tee, working with real file handles.
>


I'm not particularly familiar with this, but it seems to me that if
you're trying to catch stdout/stderr from a program you can call with
(say) popen2, you could just read from the returned stdout/stderr
pipe, and then write to a series of file handles (including
sys.stdout).

Or am I missing something? =)

~G

 
Reply With Quote
 
nathan.shair@gmail.com
Guest
Posts: n/a
 
      02-16-2007
On Feb 16, 4:07 pm, (E-Mail Removed) wrote:
> On Feb 16, 3:28 pm, "Gabriel Genellina" <(E-Mail Removed)> wrote:
>
>
>
> > That's ok inside the same process, but the OP needs to use it "from a
> > subprocess or spawn".
> > You have to use something like tee, working with real file handles.

>
> I'm not particularly familiar with this, but it seems to me that if
> you're trying to catch stdout/stderr from a program you can call with
> (say) popen2, you could just read from the returned stdout/stderr
> pipe, and then write to a series of file handles (including
> sys.stdout).
>
> Or am I missing something? =)
>
> ~G


That works, but it isn't live streaming of stdout/stderr. Most of the
time, if you stream both, one could lock the process, or have the
stdout/stderr printed in the wrong order.

 
Reply With Quote
 
Fuzzyman
Guest
Posts: n/a
 
      02-17-2007
On Feb 16, 11:37 pm, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> On Feb 16, 4:07 pm, (E-Mail Removed) wrote:
>
>
>
> > On Feb 16, 3:28 pm, "Gabriel Genellina" <(E-Mail Removed)> wrote:

>
> > > That's ok inside the same process, but the OP needs to use it "from a
> > > subprocess or spawn".
> > > You have to use something like tee, working with real file handles.

>
> > I'm not particularly familiar with this, but it seems to me that if
> > you're trying to catch stdout/stderr from a program you can call with
> > (say) popen2, you could just read from the returned stdout/stderr
> > pipe, and then write to a series of file handles (including
> > sys.stdout).

>
> > Or am I missing something? =)

>
> > ~G

>
> That works, but it isn't live streaming of stdout/stderr. Most of the
> time, if you stream both, one could lock the process, or have the
> stdout/stderr printed in the wrong order.


Everytime I've looked to do something like this (non-blocking read on
the stdout of a subprocess) I've always come back to the conclusion
that threads and queues are the only reasonable way (particularly on
windows). There may be a better solution using select.

Fuzzyman
http://www.voidspace.org.uk/python/articles.shtml

 
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
Reading console output and writing to console jan.rebada@gmail.com C++ 5 03-04-2008 09:00 AM
how to control position of output values on output console pratap C++ 3 07-19-2007 02:38 PM
Running multiple console processes and watching their output Yoav Python 1 08-22-2005 05:51 PM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
Re: console mp3->wav decoder for $indows or GUI one which supports console Ben Finney Python 2 06-30-2003 05:43 AM



Advertisments