Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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