New in version 2.0.
The atexit module defines a single function to register cleanup functions. Functions thus registered are automatically executed upon normal interpreter termination.
Note: the functions registered via this module are not called when the program is killed by a signal, when a Python fatal internal error is detected, or when os._exit() is called.
This is an alternate interface to the functionality provided by the
Note: This module is unlikely to work correctly when used with other code
sys.exitfunc. In particular, other core Python modules are
free to use atexit without the programmer's knowledge. Authors who
sys.exitfunc should convert their code to use
atexit instead. The simplest way to convert code that sets
sys.exitfunc is to import atexit and register the function
that had been bound to
|func[, *args[, **kargs]])|
At normal program termination (for instance, if sys.exit() is called or the main module's execution completes), all functions registered are called in last in, first out order. The assumption is that lower level modules will normally be imported before higher level modules and thus must be cleaned up later.
If an exception is raised during execution of the exit handlers, a traceback is printed (unless SystemExit is raised) and the exception information is saved. After all exit handlers have had a chance to run the last exception to be raised is re-raised.