-3

I have a problem with my python program, it is supposed to print each character individually with a delay in between. Here is my code:

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)

But when run in cmd it waits until the program is done THEN shows the output. How can i make this work?

*the "text" file just contains "hello"

jackedak
  • 1
  • 4

1 Answers1

1

print is buffered output. It's not immediately printed but stored and printed later. With flush=True you can flush the buffer and immediately write the buffer to the terminal.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62