1

Following the description given here how to create output in python in such a way, that the previous output is overwritten, I tried the following code

from __future__ import print_function
import time

for val in range(10):
    time.sleep(0.2)
    print(val, end='\r')

but do not see any output at all. With end=\n it works as expected (i.e. the numbers 0 to 9 in a vertical column). What is the reason I do not see any output? Do I need to flush something? Is this operating-system depended (working on Ubuntu Linux)?

Community
  • 1
  • 1
Alex
  • 41,580
  • 88
  • 260
  • 469

1 Answers1

0

I guess that the stream isn't flushed until it gets a '\n'.

Have a look at this article: Disable output buffering

Try if it works when you start Python with -u.

Community
  • 1
  • 1
cxxl
  • 4,939
  • 3
  • 31
  • 52
  • It works either with `python -u` as well as adding a `sys.atdout.flush()` in the code after the print statement. – Alex Dec 10 '12 at 10:05