Working with Files
Python makes file operations straightforward. The built-in open() function handles text and binary files. Always use the with statement โ it automatically closes the file even if an error occurs.
# Basic file operations with context manager (always use 'with')
# Writing a file
with open("hello.txt", "w") as f:
f.write("Hello, World!\n")
f.write("Second line\n")
# Reading entire file
with open("hello.txt", "r") as f:
content = f.read()
print(content)
# Reading line by line (memory-efficient for large files)
with open("hello.txt", "r") as f:
for line in f:
print(line.strip()) # strip() removes the \n
# Reading all lines as a list
with open("hello.txt", "r") as f:
lines = f.readlines()
# ["Hello, World!\n", "Second line\n"]File Modes
| Mode | Meaning | Creates? | Truncates? |
|---|---|---|---|
"r" | Read (default) | โ | โ |
"w" | Write (overwrites) | โ | โ |
"a" | Append | โ | โ |
"x" | Exclusive create (fails if exists) | โ | N/A |
"r+" | Read and write | โ | โ |
"b" | Binary mode (add to any above) | - | - |
# Append to file
with open("log.txt", "a") as f:
f.write("New log entry\n")
# Binary mode โ for images, PDFs, etc.
with open("image.jpg", "rb") as f:
data = f.read() # bytes object
with open("copy.jpg", "wb") as f:
f.write(data)The pathlib Module
Python 3.4+ includes pathlib for modern, object-oriented path manipulation. Prefer it over os.path.
from pathlib import Path
# Create path objects
p = Path("documents/report.txt")
home = Path.home() # /home/user or C:\Users\user
cwd = Path.cwd() # Current directory
# Path operations
p.name # "report.txt"
p.stem # "report"
p.suffix # ".txt"
p.parent # Path("documents")
# Build paths with / operator
data_dir = Path("data")
csv_file = data_dir / "sales" / "2024.csv"
# Read/write
path = Path("notes.txt")
path.write_text("Hello from pathlib!")
content = path.read_text()
# Check existence
path.exists()
path.is_file()
path.is_dir()
# Create directories
Path("new/nested/directory").mkdir(parents=True, exist_ok=True)
# List files
for f in Path(".").iterdir():
print(f)
for py_file in Path(".").glob("**/*.py"): # Recursive
print(py_file)
# Get file info
stat = path.stat()
stat.st_size # File size in bytes
stat.st_mtime # Last modified timestampCSV and JSON Files
import csv
# Write CSV
data = [
["Name", "Age", "City"],
["Alice", 30, "NYC"],
["Bob", 25, "LA"],
]
with open("people.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(data)
# Read CSV
with open("people.csv") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["Name"], row["Age"])
# JSON
import json
data = {"name": "Alice", "age": 30, "hobbies": ["python", "chess"]}
# Write JSON
with open("data.json", "w") as f:
json.dump(data, f, indent=2)
# Read JSON
with open("data.json") as f:
loaded = json.load(f)
# String conversions
json_str = json.dumps(data) # Dict โ JSON string
parsed = json.loads('{"x": 1}') # JSON string โ dictKey Takeaways
- Always use 'with' statement: auto-closes files, handles exceptions
- Iterate lines directly:
for line in fis memory-efficient - pathlib over os.path: modern, readable, cross-platform path handling
- csv.DictReader: read CSV rows as dicts keyed by column name
- json.dump/load for files, json.dumps/loads for strings
Practice Exercises
- Write a script that reads a text file and prints word count, line count, and the 10 most common words.
- Write a CSV reader that loads a CSV of products (name, price, quantity) and computes total inventory value.
- Write a JSON-based simple contact book: save contacts to file, load on startup, add/search/delete contacts.
- Use pathlib to find all Python files in a directory tree and count total lines of code.