install Ctrl-C handler

James Henderson james at logicalprogression.net
Wed Feb 4 13:20:02 CET 2004


On Wednesday 04 February 2004 12:15 pm, James Henderson wrote:
> On Wednesday 04 February 2004 12:04 pm, Uwe Mayer wrote:
> > Hi,
> >
> > can anybody tell me how you install something like a Ctrl-C handler in
> > Python, i.e. a routine that is called when ^C is pressed and that can do
> > cleanup work?
> >
> > Thanks
> > Uwe
>
> Maybe you require something more sophisticated but ctrl-C raises an
> exception
>
> that can be caught:
> >>> try:
>
> ...     while 1: pass
> ... except KeyboardInterrupt:
> ...     print 'Interrupted!'
> ...
> Interrupted!

Thinking about it, try-finally would also work:

>>> try:
...     while True: pass
... finally:
...     print 'cleaning up'
...
cleaning up
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
KeyboardInterrupt


J.
-- 
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
http://sourceforge.net/projects/mailmanager/





More information about the Python-list mailing list