I want to know if it's possible to catch a Control-C in python in the following manner:
if input != contr-c:
#DO THINGS
else:
#quit
I've read up on stuff with try and except KeyboardInterrupt but they're not working for me.
Consider reading this page about handling exceptions.. It should help.
As @abarnert has said, do sys.exit() after except KeyboardInterrupt:.
Something like
try:
# DO THINGS
except KeyboardInterrupt:
# quit
sys.exit()
You can also use the built in exit() function, but as @eryksun pointed out, sys.exit is more reliable.
KeyboardInterruptbut instead of just exiting, Python does the operations intry, which is not what I want. - pauliwagoinput/stdin.read/etc., a platform-specificgetch(if so, which?),curses, or …? It's generally possible in every case, but the answers are very different between the cases. - abarnerttry"? Normally, when you handle an exception, Python does the operations in theexceptblock. If you want it to quit, you can just, e.g., callsys.exit()in thatexceptblock. - abarnert