2

例如,当我运行时:

print("[",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("=",end=" ")
time.sleep(1)
print("]",end=" ")

10 秒内什么都没有发生,然后整个 [ = = = = = = = = = = ] 出现。我怎样才能防止这种情况,以便它可以充当一种进度条?

4

4 回答 4

5

每次打印后尝试刷新标准输出:

import sys

print("=",end=" ")
sys.stdout.flush()
于 2010-05-11T22:45:45.450 回答
3

实际上,进度条属于sys.stderr,它(非常方便且并非巧合)没有缓冲。所以我建议你:

print("=", end=" ", file=sys.stderr)

反而。

PS a synopsis of the standard input, output and error streams in POSIX-conformant operating systems can be found in Wikipedia: Standard streams. In a few words: stdin is the input to a process; stdout is the useful output of a process, the results; stderr is for warnings, errors and out-of-band (e.g. progress bars) output.

于 2010-05-12T01:47:22.783 回答
0

sys.stdout.flush()

于 2010-05-11T22:45:34.420 回答
0

每次要写入更新时都需要刷新stdout使用。sys.stdout.flush()

于 2010-05-11T22:45:43.510 回答