2

I'm not sure where to start with this, so even pointing me in the right direction would be helpful. I would like to update a line continuously in the terminal using python but can't figure out how to do this. I'm thinking something like top is doing with constantly updated information but not printing new lines.

So simplistically something like this:

for i in myList:
    print i #but overwrite previous output rather than putting on new line
The Nightman
  • 5,609
  • 13
  • 41
  • 74
  • [curses](https://docs.python.org/2/howto/curses.html) is the classic library to do that, but there are probably better options I'm just not thinking of at present. :) – cxw May 05 '16 at 17:11

1 Answers1

7

You can print a '\r' (which places the caret at the beginning of the current line) after any output:

for x in range(10):
    print x, '\r',
print "\n"

You can also add sys.stdout.flush() after printing to make the output visible immediately.

ForceBru
  • 43,482
  • 10
  • 63
  • 98