简体   繁体   English

为什么python3的print语句在指定end关键字时不刷新output?

[英]Why doesn't python3's print statement flush output when end keyword is specified?

from sys import argv, stdout as cout
from time import sleep as sl
print("Rewinding.......",end = '') # If end is given output isn't flushed. But why?
cout.flush()
for i in range(0,20):
    sl(0.2)
    print(".",end='',flush = True) #No output is printed if flush wasn't specified as true.

print("Done") #Output is by default flushed here

When I specified end and sleep, I noticed that output wasn't flushed until next print where it was by default flushed.当我指定结束和睡眠时,我注意到 output 直到下一次打印才被刷新,默认情况下它是被刷新的。

Why does this happen?为什么会这样? I had to manually flush the output.我不得不手动冲洗 output。

In fact this is the default behavior of the underlying stdio functions. 实际上,这是基础stdio函数的默认行为。

When the output is to console, the stream will be automatically flushed when a newline is encountered, but not other characters. 当输出到控制台时,遇到换行符时将自动刷新流,但不会遇到其他字符。

If the output is not a console, then even newline won't trigger a flush. 如果输出不是控制台,那么即使换行也不会触发刷新。

If you want to make sure about flush, you can tell the print() explicitly: 如果要确保刷新,可以显式告诉print()

print("Rewinding.......",end = '',flush=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM