atexit
The Python atexit module provides an interface for registering function to be executed upon the program’s normal termination. This can be particularly useful for cleanup activities or saving the state before exiting.
Here’s a quick example:
goodbye.py
import atexit
def goodbye():
print("Goodbye, world!")
atexit.register(goodbye)
print("Program is running...")
Run it on your command line:
$ python goodbye.py
Program is running...
Goodbye, world!
Notice how the registered function runs automatically after the program finishes.
Key Features
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
atexit.register() |
Function | Registers a function to be executed at termination |
atexit.unregister() |
Function | Removes a previously registered exit function |
Examples
Using atexit.register() as a decorator:
decorator_example.py
import atexit
@atexit.register
def goodbye():
print("Goodbye, world!")
print("Program is running...")
Run it on the command line:
$ python decorator_example.py
Program is running...
Goodbye, world!
Demonstrating LIFO (last in, first out) execution order:
lifo_example.py
import atexit
@atexit.register
def first():
print("First registered, runs last")
@atexit.register
def second():
print("Second registered, runs first")
print("Program is running...")
Run it on the command line:
$ python lifo_example.py
Program is running...
Second registered, runs first
First registered, runs last
Common Use Cases
The most common tasks for atexit include:
- Closing files and network connections
- Releasing system resources like database connections and locks
- Saving application state before exiting
- Flushing data buffers to ensure data integrity
- Logging program termination events
Real-World Example
Consider a scenario where you want to ensure that a log file is closed properly when the program exits:
log_handler.py
import atexit
log_file = open("app.log", "w")
def close_log_file():
log_file.write("Application is closing.\n")
log_file.close()
print("Log file closed.")
atexit.register(close_log_file)
log_file.write("Application has started.\n")
print("Program is running...")
Run it on the command line:
$ python log_handler.py
Program is running...
Log file closed.
$ cat app.log
Application has started.
Application is closing.
The close_log_file() function runs automatically when the program terminates, ensuring that the final log entry is written and the file is properly closed.
Related Resources
Tutorial
Primer on Python Decorators
In this tutorial, you'll look at what Python decorators are and how you define and use them. Decorators can make your code more readable and reusable. Come take a look at how decorators work under the hood and practice writing your own decorators.
For additional information on related topics, take a look at the following resources:
- Reading and Writing Files in Python (Guide) (Tutorial)
- Python Decorators 101 (Course)
- Primer on Python Decorators (Quiz)
- Reading and Writing Files in Python (Course)
- Reading and Writing Files in Python (Quiz)
By Leodanis Pozo Ramos • Updated Jan. 29, 2026