File Operations
Overview
Read and write local files safely using context managers to prevent resource leaks.
Learning Objectives
- Open, read, and write files.
- Use with statements as context managers.
- Manage file paths using pathlib.
Concept Explanation
Opening files returns file pointers. Always use the with open(...) statement; it acts as a context manager, automatically closing files when exiting the block even if exceptions occur. Always specify encoding='utf-8' explicitly.
Code Examples
Example 1 — Basics
This example introduces the fundamental syntax and concepts.
with open('notes.txt', 'w') as f:
f.write('Hello Python')
Example 2 — Everyday Usage
This example demonstrates a realistic scenario handling business parameters.
try:
# Read file line-by-line
with open('users.txt', 'r', encoding='utf-8') as f:
for line in f:
print(line.strip())
except FileNotFoundError:
print('Users file is missing.')
Example 3 — Advanced Example
This example shows clean, production-grade code structure following senior development standards.
from pathlib import Path
def save_data(filename, content):
# Pathlib handles cross-platform paths safely
dest_path = Path('output_data') / filename
dest_path.parent.mkdir(parents=True, exist_ok=True)
with open(dest_path, 'w', encoding='utf-8') as f:
f.write(content)
return f'File saved at: {dest_path}'
print(save_data('report.txt', 'Stats results: OK'))
Visual Flow
The following execution flow represents the step-by-step evaluation inside the interpreter:
Open file context → Yield file descriptor pointer → Read/write contents → Auto-close on exit
Common Mistakes
Review these common pitfalls when working with this topic:
- Opening files manually without using with statements, which leaks file descriptors.
- Loading massive files into memory all at once using f.read() instead of iterating line-by-line.
- Not specifying encoding='utf-8', causing character encoding bugs on Windows.
- Using simple string concatenation to construct file paths instead of using pathlib.
Best Practices
Enforce these Pythonic best practices in your codebase:
f = open('data.txt', 'r')
text = f.read()
f.close() # Fails to close if error happens before this line
with open('data.txt', 'r', encoding='utf-8') as f:
text = f.read() # Always closes safely
Quick Revision
Use these key summaries for last-minute revision:
- with statements close files automatically.
- Specify encoding='utf-8' for cross-platform safety.
- Read large files line-by-line to save memory.
- pathlib is the standard for path management.
- Modes: 'r' read, 'w' overwrite, 'a' append.
DataPlayArena Technical Editorial Board
Curated & maintained by enterprise data platform engineers with 10+ years in distributed systems.
Runtime Verification Standards: Every code example, query, and architecture diagram in this tutorial is compiled and verified against real runtimes (Apache Spark 3.5, Python 3.11, PostgreSQL, and DuckDB). If you spot any errata or want to suggest improvements, please submit feedback.