You can make a countdown function, for example:
import sys import time def countdown(t, step=1, msg='sleeping'): # in seconds pad_str = ' ' * len('%d' % step) for i in range(t, 0, -step): print '%s for the next %d seconds %s\r' % (msg, i, pad_str), sys.stdout.flush() time.sleep(step) print 'Done %s for %d seconds! %s' % (msg, t, pad_str)
A carriage return \r and a comma will keep printing in the same line (avoiding one line for each countdown value)
As the number of seconds decreases, pad_str will ensure that the last line is overwritten with spaces, instead of leaving the last character after cutting the output.
Final printing overwrites the last status message with the completed message and increases the output line, so there is evidence of a delay.
Saullo GP Castro
source share