Topic 7 of 64
File Handling
Overview
Python makes reading and writing files simple with built-in open() and the with statement. File handling is essential for data processing, logging, configuration management, and building CLI tools.
Syntax
python
# Reading files
with open("data.txt", "r", encoding="utf-8") as f:
content = f.read() # entire file as string
lines = f.readlines() # list of lines
for line in f: # memory efficient iteration
process(line.strip())
# Writing files
with open("output.txt", "w", encoding="utf-8") as f:
f.write("Hello, World!\n")
# Append mode
with open("log.txt", "a") as f:
f.write(f"{timestamp}: User logged in\n")
# JSON
import json
with open("config.json") as f: data = json.load(f)
with open("output.json", "w") as f: json.dump(data, f, indent=2)
# CSV
import csv
with open("sales.csv") as f:
reader = csv.DictReader(f)
for row in reader: process(row)Common Pitfalls
- Always use the with statement — it automatically closes the file even if an exception occurs.
- Default mode is 'r' (read text). Use 'rb' for binary files (images, PDFs).
- Interview tip: Use pathlib.Path instead of os.path — it's object-oriented and handles path separators cross-platform.
Real-World Example
A log parser that analyzes error rates from application logs:
example
python
from collections import Counter
from pathlib import Path
import json
def analyze_logs(log_file: str) -> dict:
error_counts = Counter()
total_requests = 0
log_path = Path(log_file)
if not log_path.exists():
raise FileNotFoundError(f"Log file not found: {log_file}")
with open(log_path, "r", encoding="utf-8") as f:
for line in f:
parts = line.strip().split(" | ")
if len(parts) < 3: continue
status_code = parts[1]
total_requests += 1
if status_code.startswith("5"):
error_counts[status_code] += 1
report = {
"total_requests": total_requests,
"error_count": sum(error_counts.values()),
"error_rate": f"{sum(error_counts.values())/total_requests*100:.2f}%",
"errors_by_code": dict(error_counts),
}
with open("report.json", "w") as f:
json.dump(report, f, indent=2)
return report