A more elegant way to flush print buffer?

Teemu Luojola teemu.luojola at pbezone.net
Mon May 26 13:36:21 EDT 2003


Is there any other way to flush the print buffer than to call 
sys.stdout.flush() ? Take as an example a simple "windmill" practice, 
like the following:

import sys

windmill = ("/", "|", "\\", "-")
j = 0
print "\n\nWhirling:  ",
sys.stdout.flush()
for i in range(1000000):
     if (i % 40000 == 0):
         print "\b\b\b", windmill[j],
         sys.stdout.flush()
         j += 1
         if (j >= 4):
             j = 0
print "\n\n"

I don't know if the need to flush is system independent or not, but at 
least in python 2.1.3 / bash / debian woody it is required. Seems like 
it is required because there is no newline after the previous print.

(Also, I'm very aware that there may be better ways to do this kind of 
windmill -- I'm not very experienced in python yet).

Teemu Luojola





More information about the Python-list mailing list