Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
608 views
Welcome To Ask or Share your Answers For Others

1 Answer

You probably need to flush the output buffer after each print invocation. See How to flush output of Python print?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...