Python sys.stdout.flush() 似乎不起作用



我想通过反复覆盖打印函数来查看我的 for 循环的进度。 我希望每次循环进行时都会覆盖打印的行。发生的事情是,在每次循环之后,都会在前一行打印的正下方打印一个新行。此问题已通过从打印行中删除 来解决,因为 每次都会覆盖一个新的空行,而不是覆盖第一个打印的行。

import sys
import time
count1=0
x=10
for i in range(x):
count1+=1
sys.stdout.write("r{} out of {}...n".format(count1, x))
sys.stdout.flush()
time.sleep(.1)

我在Windows 10上的Jupter Notebook 4.3.1中使用Python 3.6(64位(。

我已经查看了Stack Overflow上发布的几个问题,但我还没有能够解决它。

谢谢!

删除创建新行的""。

sys.stdout.write("r{} out of {}...".format(count1, x))

最新更新