lanis3333
lanis3333

Reputation: 21

Python Print statement doesn't overwrite previous line with \r

I am trying to overwrite a printed line in Python2.7

from __future__ import print_function
import time
print('This is the first statement', end='\r')
time.sleep(1)
print('This is the Second Statement')

From this I would expect the first printed line, then one second later, the second printed line, overwriting the first one.

What I get when I execute the code is just the second printed line one second after the script is run. What is going on here? How do I get the first statement to appear before the second then the overwrite?

Upvotes: 2

Views: 410

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

Use sys.stdout.flush:

from __future__ import print_function
import time
import sys
print('This is the first statement', end='\r')
sys.stdout.flush() 
time.sleep(1)
print('This is the Second Statement')

Upvotes: 2

Related Questions