【问题标题】:cmd displaying output only after program has finished [duplicate]cmd仅在程序完成后显示输出[重复]
【发布时间】:2020-11-15 19:41:27
【问题描述】:

我的 python 程序有问题,它应该单独打印每个字符,中间有延迟。 这是我的代码:

import time
import os
txt=open("text",'r').read()
delay=0.25
bg,fg=0,2
os.system("color "+str(bg)+str(fg))
for i in txt:
    print(i,end='')
    time.sleep(delay)

但是当在 cmd 中运行时,它会等到程序完成然后显示输出。 我怎样才能做到这一点?

*“文本”文件只包含“你好”

【问题讨论】:

  • 这能回答你的问题吗? How to flush output of print function?
  • 不,这对我没有帮助,他们中的任何一个
  • 使用flush=True 完全符合您的要求。您能否编辑您的问题并指出确切您想要什么?
  • 为什么不呢?你试过print(i,end='',flush=True)吗?
  • 这是做什么的?

标签: python python-3.x cmd timing


【解决方案1】:

print 是缓冲输出。它不会立即打印,而是稍后存储和打印。使用flush=True,您可以刷新缓冲区并立即将缓冲区写入终端。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 2014-10-03
    • 2016-07-30
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多