The easiest way is as follows.
import time import sys for remaining in range(10, 0, -1): sys.stdout.write("\r") sys.stdout.write("{:2d} seconds remaining.".format(remaining)) sys.stdout.flush() time.sleep(1) sys.stdout.write("\rComplete! \n")
"\r" returns the text cursor to the beginning of the line so you can overwrite what you previously write. Since usually the output is not written to a new line ( "\n" ), you need to manually .flush() output the stream.
Since the line is not cleared, we need to make sure that each new line of output is long enough to hide the existing line.
The curses module has tools for more advanced terminal output, but is more difficult to use.
source share