Reading and writing files is one of the most common tasks in programming. Python makes it straightforward, but there are a few details worth understanding well.
Opening Files
The built-in open() function is the entry point, and it should almost always be paired with a with block so the file closes automatically:
with open("notes.txt", "r") as f:
content = f.read()
File Modes
| Mode | Meaning |
|---|---|
| "r" | Read (default) — file must exist |
| "w" | Write — creates file or truncates existing one |
| "a" | Append — writes go to the end of the file |
| "x" | Exclusive create — fails if the file exists |
| "b" | Binary mode (combine with above, e.g. "rb") |
| "+" | Read and write (e.g. "r+") |
Reading Strategies
with open("data.txt") as f:
whole = f.read() # entire file as a string
with open("data.txt") as f:
lines = f.readlines() # list of lines
with open("data.txt") as f:
for line in f: # memory-efficient, one line at a time
process(line)
For large files, iterating line-by-line avoids loading the entire file into memory at once.
Writing to Files
with open("output.txt", "w") as f:
f.write("First line\n")
f.writelines(["Second line\n", "Third line\n"])
Use "a" instead of "w" if you want to add to an existing file rather than overwrite it.
Working with Binary Data
Images, PDFs, and other non-text files need binary mode:
with open("image.png", "rb") as f:
data = f.read()
with open("copy.png", "wb") as f:
f.write(data)
Encoding Matters
Text files are stored as bytes, so Python needs to know how to decode them. UTF-8 is the safe default, but always specify it explicitly for portability:
with open("notes.txt", "r", encoding="utf-8") as f:
content = f.read()
Omitting encoding relies on the platform default, which can differ between systems and cause subtle bugs.
Handling Errors Gracefully
try:
with open("missing.txt") as f:
data = f.read()
except FileNotFoundError:
print("File doesn't exist.")
except PermissionError:
print("No permission to read this file.")
Working with Paths
The pathlib module offers a cleaner, object-oriented way to handle file paths than raw strings:
from pathlib import Path
p = Path("data") / "notes.txt"
if p.exists():
text = p.read_text(encoding="utf-8")
Key Takeaway
Always use with when handling files — it guarantees proper cleanup. Be deliberate about mode, encoding, and reading strategy, especially for large files or binary data, and prefer pathlib for cleaner path manipulation.
Comments (0)
No comments yet
Be the first to share your thoughts!