Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Quick Python: Interrupts

Published on

Updated on

This post is part of a new series I’m starting where I quickly outline small Python snippets.

If you have an application that you want to gracefully exit when the user presses CTRL-C here’s the short snippet

import signal
import sys

# Function that gets called when interrupt is found
def signal_handler(sig, frame):
  print('You pressed Ctrl+C!')

# Attach the function to the interrupt signal

print('Press Ctrl+C')
Was this useful? Feel free to share: Hacker News Reddit Twitter
Reply via Email

Have you published a response to this? :