<br>You've had some good responses already, but here're two more:<br><br>1) Easiest would be to use setvbuf in the child process, if you have access to its source.  This allows you to force line-oriented buffering.<br>
<br>2) stdio likes to buffer to tty/pty's in a line-oriented manner, and other things in a block-oriented manner - by default, so users get pleasing output increments, and programs get efficiency.  To trick stdio into buffering line-oriented by default when it's sending output to another program, you may have to talk to the child process using a pty, AKA a pseudo terminal.  There are a few ways of doing this:<br>
   A) Use CPython's pty module.<br>   B) Use something like pexpect.<br>   C) Check out the pty program in comp.sources.unix volume 25.  This might be pretty easy too,<br>        assuming that pty still builds on a modern *ix.<br>
<br><div class="gmail_quote">On Wed, May 9, 2012 at 8:35 AM, Florian Lindner <span dir="ltr"><<a href="mailto:mailinglists@xgm.de" target="_blank">mailinglists@xgm.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello,<br>
<br>
how can I achieve a behavior like tee in Python?<br>
<br>
* execute an application<br>
* leave the output to stdout and stderr untouched<br>
* but capture both and save it to a file (resp. file-like object)<br>
<br>
I have this code<br>
<br>
proc = subprocess.Popen(shlex.split(cmd), stdout = subprocess.PIPE,<br>
stderr=subprocess.STDOUT)<br>
while True:<br>
    out = proc.stdout.readline()<br>
    if out == '' and proc.poll() != None:<br>
       break<br>
    sys.stdout.write(out)<br>
    logfile.write(out)<br>
<br>
This works so far but always buffers a couple of lines and outputs<br>
them en bloc. The final output is like it is desired but with a<br>
significant delay. Is there a way around that?<br>
<br>
Thanks,<br>
<br>
Florian<br>
<span class="HOEnZb"><font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br>