[Tutor] Logging stdout and stderr

Hugo González Monteverde hugonz-lists at h-lab.net
Sat Jun 18 05:27:44 CEST 2005


Hi,

Why don't you print it while you get it..?

import os, sys

logfile = open("logfile.txt", "r")
input, output = os.popen2("your command")

while True:
   temp = output.read(1) #or whatever fits you, more bytes is faster

   if not temp: //EOF reached
     break

   logfile.write(temp)
   sys.stdout.write(temp)



Connor.Smith at huntington.com wrote:
> 
> Hi there -
> 
> I'm trying to log the results of a command that I'm running via the 
> popen2 module to a file, while at the same time displaying its progress 
> on the screen. So, at the end of the run, I should have the entire 
> output (stdout and stderr) on the screen, but also have two file objects 
> each with the contents of one of stdout and stderr.  I can figure out 
> how to write the results to a file, and then process or log it, but what 
> I can't do is figure out a way to simultaneously display it to the 
> screen, kind of like how unix's "tee" utility works.
> 
> Can anyone help?
> 
> -Cs
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list