且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何刷新打印函数的输出(取消缓冲 python 输出)?

更新时间:2023-12-04 10:52:22

在 Python 3 中,print 可以采用可选的 flush 参数:

In Python 3, print can take an optional flush argument:

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

在 Python 2 中你必须这样做

In Python 2 you'll have to do

import sys
sys.stdout.flush()

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

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