Having problems with time.sleep

When I run, for example:

print("[",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("]",end=" ")

Nothing happens for 10 seconds, then the whole appears [= = = = = = = = = = =]. How can I prevent this so that it can act as a kind of progress bar?

+2
source share
4 answers

In fact, the progress bar refers to sys.stderrwhich (very conveniently and not by accident at all) is not buffered. Therefore, I suggest you:

print("=", end=" ", file=sys.stderr)

instead.

PS , POSIX : . : stdin - ; stdout - , ; stderr , (, ).

+3

stdout :

import sys

print("=",end=" ")
sys.stdout.flush()
+5
0
source

You need to clean up stdoutwith sys.stdout.flush()every time you want to write updates.

0
source

All Articles