Archive
Countdown in command line
I have a console application that runs in an eternal loop. It does something then it sleeps for 5 minutes. How to do this pause in a nice way?
(1) time.sleep(300)
The first idea is to put it to sleep for 5 minutes. It does the job but when I check the output of the script, I have no idea when it continues. So it’s not very informative.
(2) print a dot each second
Another way is to print a dot each second. If you use sys.stdout.write('.'), then don’t forget to set the output unbuffered. It’s a bit better but still not perfect.
(3) countdown
The nicest way may be a countdown that goes from 300 down to 0. Now, you don’t want to print each number in a new line, do you? How to keep the countdown in the same line?
#!/usr/bin/python2.7 -u
# -u switch: unbuffered output
def wait(sec):
while sec > 0:
sys.stdout.write(str(sec) + ' \r')
sec -= 1
time.sleep(1)
The trick is the ‘\r‘, which means “carriage return”. That is: print the number, clear with some spaces, then jump back to the beginning of the line.
Nice, but… How to get rid of the cursor? You can switch it off/on in Linux easily. Here is my complete solution:
#!/usr/bin/python2.7 -u
import os
import sys
from time import sleep
def wait(sec):
while sec > 0:
sys.stdout.write(str(sec) + ' \r')
sec -= 1
sleep(1)
def main():
os.system('setterm -cursor off')
try:
while True:
# an external command that I call regularly
os.system('./proxies.py')
wait(300)
except KeyboardInterrupt:
print
finally:
os.system('setterm -cursor on')
############################################
if __name__ == "__main__":
main()
The finally part makes sure that the cursor will be switched back on, even if the script is interrupted with CTRL+C for instance.
The same idea in a more elegant way:
class CursorOff(object):
def __enter__(self):
os.system('setterm -cursor off')
def __exit__(self, *args):
os.system('setterm -cursor on')
def wait(sec):
while sec > 0:
sys.stdout.write(str(sec) + ' \r')
sec -= 1
try:
sleep(1)
except KeyboardInterrupt:
print
return
with CursorOff():
wait(3)
I put it in a module that you can find in my jabbapylib library, here.
Blog Stats
- 1,652,052 hits
Random Post
Recent Posts
Archives
- December 2025 (1)
- December 2024 (2)
- March 2024 (1)
- February 2024 (3)
- June 2023 (1)
- December 2022 (1)
- May 2022 (1)
- March 2022 (1)
- February 2022 (2)
- October 2020 (3)
- September 2020 (2)
- July 2020 (1)
- December 2019 (1)
- November 2019 (7)
- June 2019 (2)
- May 2019 (1)
- February 2019 (1)
- January 2019 (1)
- August 2018 (1)
- July 2018 (4)
- June 2018 (5)
- May 2018 (1)
- March 2018 (2)
- February 2018 (1)
- January 2018 (2)
- December 2017 (2)
- October 2017 (1)
- August 2017 (1)
- May 2017 (2)
- April 2017 (1)
- March 2017 (1)
- February 2017 (3)
- January 2017 (5)
- December 2016 (3)
- October 2016 (2)
- September 2016 (1)
- August 2016 (4)
- July 2016 (3)
- June 2016 (2)
- May 2016 (2)
- April 2016 (1)
- March 2016 (4)
- February 2016 (1)
- January 2016 (4)
- December 2015 (5)
- November 2015 (8)
- October 2015 (2)
- September 2015 (3)
- August 2015 (6)
- July 2015 (4)
- June 2015 (1)
- May 2015 (3)
- April 2015 (2)
- February 2015 (1)
- January 2015 (6)
- December 2014 (5)
- November 2014 (2)
- October 2014 (1)
- September 2014 (1)
- August 2014 (14)
- July 2014 (3)
- June 2014 (6)
- May 2014 (3)
- April 2014 (2)
- March 2014 (4)
- February 2014 (3)
- January 2014 (19)
- December 2013 (8)
- November 2013 (9)
- October 2013 (4)
- September 2013 (10)
- August 2013 (16)
- July 2013 (4)
- June 2013 (7)
- May 2013 (7)
- April 2013 (3)
- March 2013 (12)
- February 2013 (2)
- January 2013 (10)
- December 2012 (18)
- November 2012 (3)
- October 2012 (4)
- September 2012 (5)
- August 2012 (1)
- July 2012 (1)
- May 2012 (8)
- April 2012 (9)
- March 2012 (17)
- February 2012 (3)
- January 2012 (8)
- November 2011 (11)
- October 2011 (7)
- September 2011 (17)
- August 2011 (5)
- June 2011 (1)
- May 2011 (7)
- April 2011 (21)
- March 2011 (21)
- February 2011 (7)
- December 2010 (4)
- November 2010 (1)
- October 2010 (16)
- September 2010 (15)
