all Python 3atexitterm
atexit
module
Registers functions to run when the interpreter exits normally.
Syntax
import atexit
Use it for cleanup at the end of a program: save state, close a resource, print a summary. Functions run in reverse order of registration. The mistaken expectation is that they run on abnormal exit: after os._exit, being killed by a signal or a fatal interpreter error they do not run; for cleanup within one block of code prefer with or try/finally.
Examples
import atexit
atexit.register(print, 'первым зарегистрирован — последним вызван')
atexit.register(print, 'до свидания')
print('работа') # → работа
# при выходе: «до свидания», затем «первым зарегистрирован — последним вызван»
See also
Open in the full glossarydocs.python.orgReport an inaccuracy