intermediate

File Operations

7 min readLast updated: 2026-07-12

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.

python
with open('notes.txt', 'w') as f:
    f.write('Hello Python')

Example 2 — Everyday Usage

This example demonstrates a realistic scenario handling business parameters.

python
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.

python
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:

text
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:

❌ Don't
python
f = open('data.txt', 'r')
text = f.read()
f.close() # Fails to close if error happens before this line
✅ Do
python
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.