Remove traceback in Python on Ctrl-C Remove traceback in Python on Ctrl-C python python

Remove traceback in Python on Ctrl-C


Try this:

import signalimport syssignal.signal(signal.SIGINT, lambda x, y: sys.exit(0))

This way you don't need to wrap everything in an exception handler.


import systry:    # your codeexcept KeyboardInterrupt:    sys.exit(0) # or 1, or whatever

Is the simplest way, assuming you still want to exit when you get a Ctrl+c.

If you want to trap it without a try/except, you can use a recipe like this using the signal module, except it doesn't seem to work for me on Windows..


Catch the KeyboardInterrupt:

try:    # do somethingexcept KeyboardInterrupt:    pass