On May 9, 2012, at 11:35 AM, Florian Lindner wrote:

> Hello,
> 
> how can I achieve a behavior like tee in Python?
> 
> * execute an application
> * leave the output to stdout and stderr untouched
> * but capture both and save it to a file (resp. file-like object)
> 
> I have this code
> 
> proc = subprocess.Popen(shlex.split(cmd), stdout = subprocess.PIPE,
> stderr=subprocess.STDOUT)
> while True:
>    out = proc.stdout.readline()
>    if out == '' and proc.poll() != None:
>       break
>    sys.stdout.write(out)
>    logfile.write(out)
> 
> This works so far but always buffers a couple of lines and outputs
> them en bloc. The final output is like it is desired but with a
> significant delay. Is there a way around that?
> 
> Thanks,
> 
> Florian
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Have you tried explicitly calling file.flush() on the log file? (The docs note 
that you may have to follow this with os.fsync() on some systems.)

-Bill
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to