In this lesson, you will learn how to safely and cleanly manage multiple external resources simultaneously using nested with statements and the contextlib.ExitStack utility.
The Problem: Resource Bloat
When your code needs to interact with several resources at once—such as reading from one file, writing to another, and maintaining a network connection—relying on manual cleanup (like close()) becomes error-prone. While a single with statement is standard, opening three or four resources can lead to deep indentation, often called the "Pyramid of Doom."
Nested Context Managers
The simplest way to handle multiple resources is to nest them. Python guarantees that each manager will clean up in the reverse order of initialization, ensuring that if an error occurs in the inner block, the outer resources still close correctly.
with open('input.txt', 'r') as source:
with open('output.txt', 'w') as destination:
with database_connection() as db:
data = source.read()
# Process and store data
destination.write(db.transform(data))
While clear, this nesting can quickly become difficult to read as your logic grows.
The ExitStack Solution
When you have a dynamic number of resources or want to flatten your code structure, Python provides contextlib.ExitStack. An ExitStack acts as a container for context managers. You "enter" each resource into the stack, and when the stack exits, it closes all resources in the correct order, even if exceptions occur.
from contextlib import ExitStack
filenames = ['file1.txt', 'file2.txt', 'file3.txt']
with ExitStack() as stack:
# Dynamically open multiple files
files = [stack.enter_context(open(fname, 'r')) for fname in filenames]
# Process all files together
for f in files:
print(f.read())
# All files are automatically closed here
By using ExitStack, you keep your code clean, readable, and robust, effectively preventing resource leaks without the visual clutter of deep indentation.
Key Takeaway: Use nested with statements for a small, fixed number of resources, and switch to contextlib.ExitStack when managing dynamic collections of resources to keep your code readable and maintainable.