Alex Martelli aleax at aleax.it
Tue Feb 25 11:41:23 CET 2003

gombosk at vas.birosag.hu wrote:

> How could I trap the Ctrl-C?

By running your code within a suitable try/except statement's try clause:

   except KeyboardInterrupt:
       "handle the control-C as you wish"

> My plans is:
> When I hit Ctrl-C, I would get a warning "Do you really want to quit?"

If the user confirms, you can call sys.exit.  But if the user says
they don't REALLY want to quit, you cannot begin again "from the
very point they did hit control-C" -- you can only redo the whole
try clause, by embedding the whole try/except statement in a while
loop for example.

So, make sure your main code saves state periodically as it computes,
and is able to continue computation usefully from the last saved state.


More information about the Python-list mailing list