2022-05-30-How-to-Run-Custom-Code-Upon-Hitting-Ctrl-C-in-Python.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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