Skip to content

Context Managers

Manage resources with with statements: __enter__/__exit__, contextlib, and async context managers.

What you'll learn

  • Use the with statement for resource management
  • Implement __enter__ and __exit__ protocol
  • Create context managers with contextlib.contextmanager
  • Handle exceptions in __exit__
  • Apply practical context manager patterns

Concept

The with Statement

The with statement guarantees that setup and cleanup code always runs, even if an exception occurs. This is the standard way to manage resources (files, locks, connections):

with open("data.txt") as f:
    content = f.read()
# f.close() is called automatically here, even if an error occurred

Without with, you would need a try/finally to ensure cleanup:

f = open("data.txt")
try:
    content = f.read()
finally:
    f.close()

The with statement is cleaner and guarantees cleanup — you never forget to close the resource.

The Context Manager Protocol

A context manager implements two methods:

  • __enter__() — runs at the start; its return value is assigned to the as variable
  • __exit__(exc_type, exc_val, exc_tb) — runs at the end; receives exception info if one occurred
class FileManager:
    def __init__(self, filename, mode="r"):
        self.filename = filename
        self.mode = mode
        self.file = None

    def __enter__(self):
        self.file = open(self.filename, self.mode)
        return self.file  # this is assigned to 'f' in 'with ... as f'

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.file:
            self.file.close()
        # Return False (or None) to propagate any exception
        # Return True to suppress the exception
        return False

with FileManager("data.txt") as f:
    content = f.read()

__exit__ receives three arguments describing any exception that occurred inside the with block. If no exception occurred, all three are None. Returning True from __exit__ suppresses the exception; returning False or None lets it propagate.

contextlib.contextmanager

Writing a class with __enter__/__exit__ is verbose. The @contextmanager decorator lets you write a context manager as a generator function:

from contextlib import contextmanager

@contextmanager
def open_file(filename, mode="r"):
    f = open(filename, mode)
    try:
        yield f  # this is the 'as' value
    finally:
        f.close()  # cleanup always runs

with open_file("data.txt") as f:
    content = f.read()

Everything before yield is the setup (__enter__). The yield value is assigned to the as variable. Everything after yield is the cleanup (__exit__). Use try/finally around the yield to ensure cleanup runs even on exception.

Practical: Timing Context Manager

import time
from contextlib import contextmanager

@contextmanager
def timer(label="Operation"):
    start = time.perf_counter()
    try:
        yield
    finally:
        elapsed = time.perf_counter() - start
        print(f"{label} took {elapsed:.4f}s")

with timer("Sorting"):
    sorted(range(1_000_000, 0, -1))
# Sorting took 0.1234s

Practical: Database Transaction

@contextmanager
def transaction(db):
    db.begin()
    try:
        yield db
        db.commit()
    except Exception:
        db.rollback()
        raise

with transaction(db) as conn:
    conn.execute("INSERT INTO users ...")
    conn.execute("INSERT INTO orders ...")
# Commits if no error, rolls back if an exception occurred

This is how ORMs like Django and SQLAlchemy manage transactions — the with block commits on success and rolls back on any exception.

contextlib.suppress

Suppress specific exceptions without a try/except:

from contextlib import suppress

# Instead of:
try:
    os.remove("temp.txt")
except FileNotFoundError:
    pass

# Use:
with suppress(FileNotFoundError):
    os.remove("temp.txt")

contextlib.redirect_stdout

Redirect print() output (useful for testing):

from contextlib import redirect_stdout
import io

buffer = io.StringIO()
with redirect_stdout(buffer):
    print("This goes to the buffer, not the console")
captured = buffer.getvalue()

Nested Context Managers

You can nest with statements or use ExitStack for dynamic nesting:

# Nested with
with open("input.txt") as infile, open("output.txt", "w") as outfile:
    for line in infile:
        outfile.write(line.upper())

# ExitStack for a dynamic number of files
from contextlib import ExitStack

files = ["a.txt", "b.txt", "c.txt"]
with ExitStack() as stack:
    handles = [stack.enter_context(open(f)) for f in files]
    # all files are closed when the block exits

Exception Handling in __exit__

class SafeDivision:
    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type is ZeroDivisionError:
            print("Caught division by zero — suppressing")
            return True  # suppress the exception
        return False  # let other exceptions propagate

with SafeDivision():
    result = 10 / 0  # exception is suppressed
print("Continues normally")

Returning True from __exit__ suppresses the exception — the with block continues as if nothing happened. Use this sparingly, as it can hide bugs.

Example

              import time
from contextlib import contextmanager, suppress

# Class-based context manager
class FileManager:
    def __init__(self, filename, mode="w"):
        self.filename = filename
        self.mode = mode
        self.file = None

    def __enter__(self):
        self.file = open(self.filename, self.mode)
        return self.file

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.file:
            self.file.close()
        return False

with FileManager("test.txt", "w") as f:
    f.write("Hello, context managers!")
print("File written and closed automatically")

# contextlib.contextmanager — the easy way
@contextmanager
def timer(label="Operation"):
    start = time.perf_counter()
    try:
        yield
    finally:
        elapsed = time.perf_counter() - start
        print(f"  {label} took {elapsed:.6f}s")

with timer("Sorting"):
    sorted(range(100000, 0, -1))

# suppress — ignore specific exceptions
import os
with suppress(FileNotFoundError):
    os.remove("nonexistent_file.txt")  # no error raised
print("Suppressed FileNotFoundError successfully")

# Nested context managers
with open("test.txt", "w") as f1, open("test2.txt", "w") as f2:
    f1.write("file 1 content")
    f2.write("file 2 content")
print("Both files written and closed")

# Practical: indented output
@contextmanager
def indent(level=2):
    print(" " * level + "Start of block")
    try:
        yield
    finally:
        print(" " * level + "End of block")

with indent():
    print("    Inside the block")

# Exception suppression in __exit__
class SafeDivision:
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type is ZeroDivisionError:
            print("  Caught and suppressed ZeroDivisionError")
            return True
        return False

with SafeDivision():
    result = 10 / 0
print("Continued after suppressed exception")
            

Shows a class-based FileManager, the @contextmanager decorator for a timer, suppress() for ignoring exceptions, nested with statements, a custom indent context manager, and exception suppression in __exit__.

Try it

  • JSON Formatter

    File and database context managers are used when reading/writing JSON.

  • CSV to JSON

    File context managers manage CSV file handles during conversion.

Common mistakes

The mistake

Forgetting the try/finally around yield in a @contextmanager function

The fix

If an exception occurs inside the with block and yield is not in a try/finally, the cleanup code after yield never runs. Always wrap yield in try/finally to guarantee cleanup: try: yield value finally: cleanup().

The mistake

Returning True from __exit__ unintentionally, which suppresses all exceptions

The fix

__exit__ returning True suppresses the exception that occurred in the with block. This hides bugs. Only return True when you intentionally want to suppress a specific exception. Return False or None to let exceptions propagate normally.

Related Cheatsheets

Practice Context Managers

2 exercises

Practice Context Managers

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors