Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkutaj/dda036c443624c8d40d216f7e6d9a863 to your computer and use it in GitHub Desktop.
Save pkutaj/dda036c443624c8d40d216f7e6d9a863 to your computer and use it in GitHub Desktop.
2022-05-30-How-to-Run-Custom-Code-Upon-Hitting-Ctrl-C-in-Python.py
def main():
try:
# DO SOMETHING....
except KeyboardInterrupt:
# UPON CTRL+C ASK IF A FILE CONTENTS SHOULD BE DROPPED
if input("~~> Interrupted ! Clean-up 'output.txt'? (y/N): ") == "y":
# RUN DROPPING FUNCTION IF YES
delete_output_content()
# AND SAY GOOD BYE
sys.exit("bye")
else:
# OR JUST SAY GOOD BYE
sys.exit("bye")
"""EXAMPLE OF HITTING CTRL+C
~~> Interrupted ! Clean-up 'output.txt'? (y/N):
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment