0

I want to have a simple counter on a line that changes. I have the following:

from __future__ import print_function
from time import sleep
for i in range(400):
    print("\r" + str(i), end = "")
    sleep(0.5)

In Python 3, it works fine, with the counter incrementing on the same line, but in Python 2, nothing displays. How can I get it to display in Python 2?


EDIT: I am inclined towards solutions that work for both Python 2 and 3.

d3pd
  • 7,935
  • 24
  • 76
  • 127
  • works for me just ensure that the (output is unbuffered)[http://stackoverflow.com/questions/107705/disable-output-buffering] – keety May 10 '15 at 18:50

1 Answers1

1

This should do what you want:

from sys import stdout
from time import sleep
for i in range(400):
    print"\r"+str(i),
    stdout.flush()
    sleep(0.5)
FLIR31207
  • 73
  • 1
  • 7