1

I wish to show output on the screen, and then update that output without typing next to it or below it. In other words, by changing the existing text on the console, like progress % bars on some console applications.

How can I do this in python?

jgillich
  • 71,459
  • 6
  • 57
  • 85
Kartopukus
  • 143
  • 2
  • 10
  • possible duplicate of [Replace console output in python](http://stackoverflow.com/questions/6169217/replace-console-output-in-python) – Seanny123 Apr 21 '14 at 09:57
  • If you're on a Unix-like system, you may want to see if [the `curses` module](https://docs.python.org/3/library/curses.html) does what you want to do. – Blckknght Apr 21 '14 at 10:17

1 Answers1

1

You can't really do that straight-away in the console. Your best bet would be to clear the screen after every print as follows:

import os,time

clear = lambda: os.system('cls')      # or os.system('clear') for Unix

for i in range(10,0,-1):
    clear()
    print i
    time.sleep(1)

This will give you a countdown in the console with text being written in the same place.

Alternatively, as shown here, you can do:

for i in range(10,0,-1):
    print '\r'+str(i),     #or print('\r'+str(i), end='') for python-3.x
    time.sleep(1)
Community
  • 1
  • 1
sshashank124
  • 31,495
  • 9
  • 67
  • 76