Catching KeyboardInterrupt in Python during program shutdown
问题 I'm writing a command line utility in Python which, since it is production code, ought to be able to shut down cleanly without dumping a bunch of stuff (error codes, stack traces, etc.) to the screen. This means I need to catch keyboard interrupts. I've tried using both a try catch block like: if __name__ == '__main__': try: main() except KeyboardInterrupt: print 'Interrupted' sys.exit(0) and catching the signal itself (as in this post): import signal import sys def sigint_handler(signal,