Jun-05-2024, 12:41 PM
How about using ctrl+c? If you are running from a console, ctrl+c raises a KeyboardInterrupt that your program can catch using try/except. Works for windows, osx, linux.
from time import sleep print("Nighty night") try: sleep(10) # Standin for whatever work your script does except KeyboardInterrupt: pass print("Time to wake up") # Standin for the cleanup.