The 'with' Statement and Resource Management

RoadmapsPython

Overview

The `with` statement wraps the execution of a block of code within a context manager. It guarantees that initialization happens before the block runs, and cleanup occurs automatically when the block finishes. with open('data.txt', 'w') as file: file.write('Safe and sound!') This automatic cleanup happens even if an exception is raised inside the block. Mental model: The `with` statement is an automated bouncer. It ensures you do the setup when you enter the club, and perfectly executes the teardown when you leave, no matter how you exit. with open('missing.txt', 'r') as file: # Trap: FileNotFoundError occurs before entering print(file.read()) Trap: If the setup step raises an exception (like `FileNotFoundError`), the block is never entered. The cleanup guarantee only applies if the resource is successfully acquired first. This is covered fully in context-managers.

Manual resource management using `try...finally` is verbose and error-prone. The `with` statement prevents resource leaks, such as leaving files or database connections open. It centralizes the cleanup logic into a single reliable mechanism.

Where used: Opening files using `open()`, Managing database transactions, Acquiring and releasing `threading` or `asyncio` locks

Why learn this

Code walkthrough

with open('test.txt', 'w') as f:
  f.write('Data')

print(f.closed)

Focus: `print(f.closed)` outputs `True` because exiting the indentation level automatically closes the file.

Aha moment

try:
  with open('test.txt', 'w') as f:
    raise RuntimeError('Crash!')
except RuntimeError:
  pass

print(f.closed)

Prediction: Will the file be closed (`True`) or left open (`False`) because of the crash?

Common guess: `False`, because the program crashed before it could reach the end of the block.

It prints `True`. The context manager immediately closes the file during its teardown phase, and then lets the `RuntimeError` continue bubbling up. The cleanup is guaranteed.

Common mistakes

Glossary

context manager
An object that defines what needs to be set up before a block of code runs and what needs to be cleaned up after. Example: `open('file.txt')`.
resource leaks
When a program fails to release system resources like files or memory, eventually causing the system to slow down or crash. Example: forgetting `file.close()`.

Recall questions

Understanding checks

What is printed when this code is executed?

ValueError

Once the `with` block exits, the file `f` is automatically closed. Attempting to write to a closed file raises a `ValueError`.

If a `ZeroDivisionError` occurs in the middle of a `with open(...) as f:` block, does the file remain open?

No, it is closed.

The `with` statement guarantees that the teardown process (like closing the file) executes even if the block exits via an unhandled `ZeroDivisionError`.

What is printed when this code is executed?

Caught error

The `FileNotFoundError` occurs during `open()` before the `with` block is entered. Since the context was never successfully acquired, there is nothing to clean up.

What is printed when this code is executed?

Caught error

The `FileNotFoundError` occurs during `open()` before the `with` block is entered. Since the context was never successfully acquired, there is nothing to clean up.

Practice tasks

Convert to Context Manager

Given this manual file writing code, modify it to use a `with` statement so that the file is safely closed automatically.

Challenge

Safely Reading Data

Write code to open an existing file named `data.txt` in read mode using a `with` statement, read its contents into a variable named `content`, and then let the block exit.

Return to Python Roadmap