File i/o and Context managers

AI applications constantly read and write files: loading documents for RAG, saving generated code, reading configs. The with statement ensures files are always properly closed, even if an error occurs.

Context manager lifecycle

How with statements guarantee cleanup even on errors

file_io.py
python
# Writing a file with the "with" statement (context manager)
with open("my_file.txt", "w") as f:
    f.write("Line 1: Hello from Python!\n")
    f.write("Line 2: This is a test file.\n")
print("File written")

# Reading a file
with open("my_file.txt", "r") as f:
    content = f.read()
print(content)

# Reading line by line
with open("my_file.txt", "r") as f:
    lines = f.readlines()
    for i, line in enumerate(lines):
        print(f"  Line {i}: {line.strip()}")

The with statement automatically closes the file when the block ends. "w" mode writes (overwrites), "r" mode reads. .read() gets the whole file, .readlines() gets a list of lines.

The with statement guarantees the file gets closed even if an error happens inside the block. Without it, a crash could leave the file locked. This pattern (called a context manager) is used for files, database connections, and any resource that needs cleanup.

pathlib is the modern way to handle file paths in Python. Instead of string concatenation like "data/" + "file.txt", it provides an object-oriented API where paths are objects with helpful methods. Most AI agent code uses pathlib over raw strings.

file_io.py
python
# pathlib: the modern way (used in AI agent code)
from pathlib import Path

# Write and read with pathlib (much simpler!)
output_path = Path("sample_output.txt")
output_path.write_text("Written with pathlib!\n")
text = output_path.read_text()
print(f"Read back: {text}")

# Useful path operations
some_path = Path("data/reports/summary.csv")
print(f"Name: {some_path.name}")      # summary.csv
print(f"Parent: {some_path.parent}")  # data/reports
print(f"Suffix: {some_path.suffix}")  # .csv

# Create directories (with parents)
# Path("output/charts").mkdir(parents=True, exist_ok=True)

pathlib provides an object-oriented API for file paths. .read_text() and .write_text() are one-liners. .name, .parent, and .suffix extract path components.

Quiz: Quiz

Loading practice…

Ordering exercise: File processing pipeline

Loading practice…

Validation checklist: File i/o checklist

Loading practice…