Problems with sys.stout.flush()

Joel Ross joelc at cognyx.com
Sat May 23 01:33:00 EDT 2009


Hi all,

I'm using python 2.5 and trying to flush the sys.stout buffer with 
sys.stout.flush(), but doesn't seem to work. Each time a line is printed 
   it appends the one before it I need to clear the output and write a 
new output without appending the previous one. I have tried the -u 
(unbuffered) option for python and a few examples from this site 
http://stackoverflow.com/questions/107705/python-output-buffering

Code:

import sys
from time import sleep

def progress(number, total,  char):

     percentage = float(number*100)/total
     percentage = int(round(percentage))
     percentage = int(100 - percentage)
     if percentage > 0:
         char = char * percentage
         sys.stdout.write(char)
         sys.stdout.flush()      #Not working correctly
         sleep(2)

progress(40, 50, "*")
progress(30, 50, "*")
progress(20, 50, "*")
progress(10, 50, "*")
progress(2, 50, "*")

Regards

jross



More information about the Python-list mailing list