Kartopukus
Kartopukus

Reputation: 153

Python non-scrolling console output

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?

Upvotes: 1

Views: 6526

Answers (1)

sshashank124
sshashank124

Reputation: 32189

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)

Upvotes: 1

Related Questions