22

I have a Python script and I want to make it display a increasing number from 0 to 100% in the terminal. I know how to print the numbers on the terminal but how can I "rewrite" them so 0 turns into 1, 1 into 2, and so on until 100?

4 Answers 4

26

Printing a carriage return (\r) without a newline resets the cursor to the beginning of the line, making the next print overwriting what's already printed:

import time
import sys
for i in range(100):
    print i,
    sys.stdout.flush()
    time.sleep(1)
    print "\r",

This doesn't clear the line, so if you try to, say, print decreasing numbers using this methods, you'll see leftover text from previous prints. You can work around this by padding out your output with spaces, or using some of the control codes in the other answers.

Sign up to request clarification or add additional context in comments.

4 Comments

is this platform independent?
@klez: probably not for all terminals. It works for typewriters. Is that platform independent enough for you? :)
On the two platforms at my disposal (Linux and Windows), it works.
I don't have access to Windows at the moment. Can you please test if the recipe in my answer works there? If you don't have the time don't bother, I am sure I will come across a Windows machine soon.
5

This recipe here should prove useful. Using that module as tc, the following code does what you want:

from tc import TerminalController
from time import sleep
import sys

term = TerminalController()

for i in range(10):
    sys.stdout.write("%3d" % i)
    sys.stdout.flush()
    sleep(2)
    sys.stdout.write(term.BOL + term.CLEAR_EOL)

The recipe uses terminfo to get information about the terminal and works in Linux and OS X for a number of terminals. It does not work on Windows, though. (Thanks to piquadrat for testing, as per the comment below).

Edit: The recipe also gives capabilities for using colours and rewriting part of the line. It also has a ready made text progress bar.

2 Comments

as requested, I tried your code on Windows (Windows 7, Python 2.7). Output is " 0 1 2 3 4 5 6 7 8 9".
Pulling in terminfo is overly complex for something as basic as send carriage return, but I give a thumbs-up anyway for correctness :)
3

Using the blessings package - clear your screen (clear/cls) and enter:

import sys
from blessings import Terminal
from time import sleep # <- boy, does this sound tempting a.t.m.

term = Terminal()
for i in range(6):
    with term.location(term.width - 3, term.height - 3):        
        print('{}'.format(i))
    sleep(2)
    if (i == 3):
        print('what was I doing, again?')
print('done')

To install it from CheeseShop, just...

pip install blessings

Comments

1

Based on this answer, but without the terminal controller:

import time
import sys
for i in range(100):
    sys.stdout.write("Downloading ... %s%%\r" % (i))
    sys.stdout.flush()
    time.sleep(1)

Tested on GNOME terminal (Linux) and Windows console.

Tip: Don't run this example in IDLE editor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.