How to better control print and stdout?

Jeff Epler jepler at unpythonic.net
Sat Mar 15 00:24:39 CET 2003


You need to call the flush method on sys.stdout, or set it unbuffered.
In my example, I also use sys.stdout.write instead of print, and \r to move
the cursor back to the beginning of the line.  but sys.stdout.flush() is
the key.

Jeff

import time, sys
def spinner(rotations, interval):
    for i in range(rotations):
        sys.stdout.write("|/-\\"[i % 4] + "\r"); sys.stdout.flush()
        time.sleep(interval)
    sys.stdout.write("\b\b"); sys.stdout.flush()





More information about the Python-list mailing list