Skip to content Skip to sidebar Skip to footer

Problems With Sys.stdout.write() With Time.sleep() In A Function

What I wanted is printing out 5 dots that a dot printed per a second using time.sleep(), but the result was 5 dots were printed at once after 5 seconds delay. Tried both print and

Solution 1:

You need to flush after writing.

sys.stdout.write('foo')
sys.stdout.flush()
wastetime()
sys.stdout.write('bar')
sys.stdout.flush()

Solution 2:

You should use sys.stderr.write for progress bars; stderr has the (not at all coincidental) advantage of not being buffered, so no sys.stderr.flush calls are needed.

See also this answer.

Post a Comment for "Problems With Sys.stdout.write() With Time.sleep() In A Function"