I'm doing a progressbar for my uploading script and therefor i want to print a row of multiple '#' but i can't get it to work. When i tell Python to not add newline it does remove it but it doesn't work as expected under functions. Using "print ('#', end='')" in Python 3 or "print '#'," in Python 2 removes it but when executed under a functions it doesn't print anything out until the whole function is finished, it should not wait just like normal print.
import time
i = 0
def status():
print('#', end='')
while i < 60:
status()
time.sleep(1)
i += 1
This should print '#' every second but it doesn't. It prints them all after 60 seconds. Using just print('#') prints it out every second as expected. I really need a fix for this. Please help!
Solution:
"sys.stdout.flush()" after each print :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…