Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

Print without newline under function doesn't work as it should in Python

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...