如何刷新打印功能的输出? [英] How to flush output of print function?

查看:83
本文介绍了如何刷新打印功能的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这不是禁用输出缓冲的重复项-链接的问题正在尝试无缓冲输出,这是更一般的.对于这个问题,最重要的答案太过强大或牵扯太多(对于这个问题来说,答案不是很好),这个问题可以由相对较新的人在Google上找到.

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.

推荐答案

在Python 3上, print 可以接受可选的flush参数

On Python 3, print can take an optional flush argument

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

在Python 2上,您必须这样做

On Python 2 you'll have to do

import sys
sys.stdout.flush()

调用print后的

.默认情况下, print 打印到 sys.stdout (有关 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆