printing unfinished line

David Fisher python at rose164.wuh.wustl.edu
Sat Mar 25 12:49:32 EST 2000


----- Original Message -----
From: "Gang Seong Lee" <gslee111 at daisy.kwangwoon.ac.kr>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Saturday, March 25, 2000 1:08 AM
Subject: printing unfinished line


> Is there any way that I can see the output of a string not having a new
line
> char.
> For example,
>
> import time
>
> print 'abc',
> time.sleep(3)
> print 'def'
>
> 'abc' is not displayed on the screen until it encounters last print 'def',
> which has new line at the end of the line. sys.write('abc') has the same
> effect.
>
> Is it possible I can see 'abc' before it goes to print 'def' ?

Yes.  Stdout (and strerr) is buffered in Python by default.  Turn it off
with the command line switch -u, or explicitly flush the buffer with
sys.stdout.flush().  Like so:

import time
import sys

print 'abc',
sys.stdout.flush()
time.sleep(3)
print 'def'

You can also flush things like an open file descriptor in order to commit a
file write.

Good luck,
David





More information about the Python-list mailing list