Skip to main content

Python File Handling

Fundamentals Deep Dive

Python File Handling

Learn how to interact with text and binary files using context managers, buffering, and the modern pathlib API.

The with statement

with open("notes.txt", "r", encoding="utf-8") as fh:
data = fh.read()
  • Ensures files close automatically.
  • Always specify encoding for text files (utf-8 recommended).

Modes

Modemeaning
rRead (default)
wWrite (truncates file)
aAppend
bBinary
+Read/write

Combine flags ("rb", "w+") as needed.

Pathlib for readable paths

from pathlib import Path

data_dir = Path("data")
for path in data_dir.glob("*.csv"):
print(path.name)

Benefits:

  • Works cross-platform.
  • Provides handy helpers (Path.read_text(), Path.write_text()).
  • Supports path arithmetic (reports / "2024" / "summary.json").

Structured data

import json

with open("config.json") as fh:
config = json.load(fh)

with open("config.json", "w") as fh:
json.dump(config, fh, indent=2)

Likewise, use csv.DictReader, csv.DictWriter, and yaml.safe_load (with PyYAML) for other formats.

Binary files

with open("photo.jpg", "rb") as fh:
chunk = fh.read(1024)

Use binary mode for images, executables, or any non-text data.

Error handling

  • Wrap file operations in try/except to catch FileNotFoundError, PermissionError, etc.
  • Provide helpful messages and fallback paths.

Next up in your learning path

Frequently Asked Questions

When should I use `open` vs `Path.read_text()`?

`Path.read_text()` and `.write_text()` are convenience wrappers around `open`. Use them for simple reads/writes; drop to `open` when you need fine-grained control over buffering or binary data.

How do I handle large files without loading everything into memory?

Stream line by line (`for line in fh:`) or read in chunks. You can also use generators to process data lazily.

Is it safe to hardcode file paths?

Avoid hardcoded absolute paths. Use configuration files, environment variables, or CLI arguments to make scripts portable.