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:

Python goodbye.py
import atexit

def goodbye():
    print("Goodbye, world!")

atexit.register(goodbye)
print("Program is running...")

Run it on your command line:

Shell
$ python goodbye.py
Program is running...
Goodbye, world!

Notice how the registered function runs automatically after the program finishes.

Key Features

  • Register functions to be executed upon program termination
  • Execute registered functions in LIFO (last in, first out) order
  • Use register() as a decorator or a regular function call
  • Pass arguments to registered functions
  • Unregister functions when they’re no longer needed

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:

Python decorator_example.py
import atexit

@atexit.register
def goodbye():
    print("Goodbye, world!")

print("Program is running...")

Run it on the command line:

Shell
$ python decorator_example.py
Program is running...
Goodbye, world!

Demonstrating LIFO (last in, first out) execution order:

Python 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:

Shell
$ 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:

Python 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:

Shell
$ 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.

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.

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Jan. 29, 2026