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
263 views
in Technique[技术] by (71.8m points)

python - 如何刷新打印功能的输出?(How to flush output of print function?)

How do I force Python's print function to output to the screen?

(如何强制将Python的打印功能输出到屏幕?)

This is not a duplicate of Disable output buffering - the linked question is attempting unbuffered output, while this is more general.

(这不是“ 禁用输出缓冲”的重复项-链接的问题正在尝试无缓冲输出,尽管这更普遍。)

The top answers in that question are too powerful or involved for this one (they're not good answers for this), and this question can be found on Google by a relative newbie.

(对于这个问题,最重要的答案太过强大或牵扯太多(对于这个问题,它们不是很好的答案),这个问题可以由相对新手在Google上找到。)

  ask by Walter Nissen translate from so

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

1 Reply

0 votes
by (71.8m points)

On Python 3, print can take an optional flush argument

(在Python 3上, print可以使用可选的flush参数)

print("Hello world!", flush=True)

On Python 2 you'll have to do

(在Python 2上,您必须做)

import sys
sys.stdout.flush()

after calling print .

(在调用print 。)

By default, print prints to sys.stdout (see the documentation for more about file objects ).

(默认情况下, print打印到sys.stdout (有关文件对象的更多信息,请参阅文档)。)


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

...