Python File Handling
Working with files is one of the most practical skills in Python — reading
configuration files, writing logs, processing CSV data, saving JSON responses, and managing project assets all
depend on file I/O. Python makes this elegant with the built-in open()
function, multiple file modes, and the with statement that guarantees
files are always closed. This chapter covers reading, writing, appending, binary files, CSV, JSON, and modern
pathlib patterns used in professional Python projects.
open() Function & File Modes
Python's built-in open(file, mode, encoding)
function opens a file and returns a file object. The mode argument controls whether you're
reading, writing, appending, or working with binary data.
FileNotFoundError if file doesn't exist. File pointer at start.FileExistsError
if file already exists. Safe creation."rb", "wb". Returns bytes instead of str. For images, PDFs, etc."r+" (read+write, no truncate),
"w+" (write+read, truncates).
# open() syntax:
# open(file, mode='r', encoding=None, errors=None)
# ✅ Always use with statement — auto-closes the file
with open("hello.txt", "w", encoding="utf-8") as f:
f.write("Hello, World!\n")
f.write("Python file handling.\n")
# Without with (less safe — you must close manually)
f = open("hello.txt", "r", encoding="utf-8")
print(f.read())
f.close() # must call manually!
# Check if file is closed
print(f.closed) # True — after close()
# Always specify encoding for text files
# encoding="utf-8" — recommended for all platforms
# encoding="utf-8-sig" — for UTF-8 with BOM (Windows Excel CSVs)
Reading Files — All Methods
Python provides multiple ways to read file content — the right choice depends on file size and how you want to process the data.
# ── read() — entire file as one string ───────────────
with open("data.txt", "r", encoding="utf-8") as f:
content = f.read() # entire file → one str
print(content)
# read(n) — read n characters
with open("data.txt", "r", encoding="utf-8") as f:
chunk = f.read(100) # first 100 characters
# ── readline() — one line at a time ──────────────────
with open("data.txt", "r", encoding="utf-8") as f:
first = f.readline() # first line (incl. \n)
second = f.readline() # second line
# ── readlines() — all lines as a list ────────────────
with open("data.txt", "r", encoding="utf-8") as f:
lines = f.readlines() # → ['line1\n', 'line2\n', ...]
print(lines[0].strip()) # strip \n
# ── Best: iterate line-by-line (memory efficient) ────
with open("data.txt", "r", encoding="utf-8") as f:
for line in f: # ← reads one line at a time
print(line.strip())
# ── File position control ─────────────────────────────
with open("data.txt", "r", encoding="utf-8") as f:
f.read(10)
print(f.tell()) # current position: 10
f.seek(0) # go back to start
print(f.read(5)) # reads first 5 chars again
# ── Handle FileNotFoundError ─────────────────────────
try:
with open("missing.txt") as f:
print(f.read())
except FileNotFoundError:
print("File not found!")
except PermissionError:
print("Permission denied!")
| Method | Returns | Best for |
|---|---|---|
f.read() |
One str (entire file) | Small files, full content needed |
f.read(n) |
n characters as str | Streaming / chunked reading |
f.readline() |
One line as str | Processing line by line manually |
f.readlines() |
List of all lines | When you need a list of lines |
for line in f: |
Iterate line by line | ✅ Best — memory-efficient for large files |
Writing & Appending to Files
Use mode "w" to write (creates or overwrites) and
"a" to append (adds to end, preserves existing content). Always use "x" if you want to create a file and fail if it already exists.
# ── Write mode "w" — creates or OVERWRITES ───────────
with open("output.txt", "w", encoding="utf-8") as f:
f.write("Line 1\n") # write() doesn't add \n automatically
f.write("Line 2\n")
f.writelines(["A\n", "B\n", "C\n"]) # write list at once
# ── Append mode "a" — adds to END ────────────────────
with open("output.txt", "a", encoding="utf-8") as f:
f.write("Appended line\n") # adds to existing file
# ── Exclusive create "x" — fails if exists ───────────
try:
with open("new_file.txt", "x", encoding="utf-8") as f:
f.write("Safe creation\n")
except FileExistsError:
print("File already exists!")
# ── print() to file ───────────────────────────────────
with open("log.txt", "w", encoding="utf-8") as f:
print("Hello from print!", file=f) # ← adds \n automatically
print("Second line", file=f)
# ── Log writing pattern ───────────────────────────────
from datetime import datetime
def log_event(message, log_file="app.log"):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(log_file, "a", encoding="utf-8") as f:
f.write(f"[{timestamp}] {message}\n")
log_event("Application started")
log_event("User logged in: alice")
"w" destroys all content
instantly — before you write anything. To add content without deleting existing data, use "a" (append). To read and write without destroying, use "r+".
The with Statement — Safe File Handling
The with statement is Python's way to guarantee that
a file is closed — even if an exception occurs inside the block. It uses the context manager protocol (__enter__ / __exit__). Always use it for
file I/O.
# ✅ with statement — file always closed, even on exception
with open("data.txt", "r", encoding="utf-8") as f:
content = f.read()
# even if read() raises an error, f.close() is called
print(f.closed) # True — guaranteed closed
# ── Multiple files in one with ───────────────────────
with (open("input.txt", "r", encoding="utf-8") as src,
open("output.txt", "w", encoding="utf-8") as dst):
for line in src:
dst.write(line.upper()) # copy uppercased
# ── Without with — risky! ────────────────────────────
f = open("data.txt")
try:
content = f.read()
finally:
f.close() # ← must do this yourself
# The with statement does this automatically — use it!
# ── Practical: copy a file ────────────────────────────
def copy_file(src_path, dst_path, chunk_size=4096):
"""Copy a file in chunks — handles large files."""
with (open(src_path, "rb") as src,
open(dst_path, "wb") as dst):
while True:
chunk = src.read(chunk_size)
if not chunk: break
dst.write(chunk)
copy_file("source.pdf", "backup.pdf")
Binary Files — Images, PDFs & Bytes
Open binary files with mode "rb" (read bytes) or
"wb" (write bytes). Operations return bytes instead of str — never use encoding with binary mode.
# ── Read binary file ─────────────────────────────────
with open("image.png", "rb") as f:
data = f.read() # returns bytes object
print(type(data)) # <class 'bytes'>
print(data[:8]) # PNG magic bytes: b'\x89PNG\r\n\x1a\n'
print(len(data)) # file size in bytes
# ── Write binary file ─────────────────────────────────
with open("copy.png", "wb") as f:
f.write(data)
# ── Encode/decode text ←→ bytes ──────────────────────
text = "Hello, Python! 🐍"
raw = text.encode("utf-8") # str → bytes
print(type(raw)) # <class 'bytes'>
decoded = raw.decode("utf-8") # bytes → str
print(decoded) # Hello, Python! 🐍
# ── base64 encoding (for sending binary over HTTP) ───
import base64
with open("image.png", "rb") as f:
b64 = base64.b64encode(f.read()).decode()
print(b64[:30], "...") # base64 string
CSV & JSON Files
Python's csv and json standard library modules make working with structured data files simple
and reliable — far better than trying to parse them manually.
# ── CSV — Comma-Separated Values ─────────────────────
import csv
# Write CSV
students = [
["Name", "Age", "Grade"],
["Alice", 20, "A"],
["Bob", 21, "B"],
["Carol", 19, "A+"],
]
with open("students.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerows(students) # write all rows at once
# Read CSV as lists
with open("students.csv", newline="", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
print(row) # ['Alice', '20', 'A']
# Read CSV as dicts (header row becomes keys)
with open("students.csv", newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["Name"], row["Grade"])
# ── JSON ─────────────────────────────────────────────
import json
data = {
"name": "Alice",
"age": 25,
"skills": ["Python", "Django", "SQL"],
"active": True,
}
# Write JSON to file
with open("user.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
# Read JSON from file
with open("user.json", encoding="utf-8") as f:
loaded = json.load(f)
print(loaded["name"]) # Alice
print(loaded["skills"][0]) # Python
pathlib — Modern File Paths
The pathlib module (Python 3.4+) provides an
object-oriented approach to file system paths — more readable and powerful than string-based os.path. pathlib.Path objects have methods
like .read_text(), .write_text(),
.glob() and .mkdir() built-in.
from pathlib import Path
# ── Create path objects ───────────────────────────────
p = Path("data") / "users" / "alice.txt"
print(p) # data/users/alice.txt
print(p.name) # alice.txt
print(p.stem) # alice (no extension)
print(p.suffix) # .txt
print(p.parent) # data/users
# ── Check existence ───────────────────────────────────
print(p.exists()) # True / False
print(p.is_file()) # True if it's a file
print(p.is_dir()) # True if it's a directory
# ── Read and write — much simpler than open() ─────────
config = Path("config.txt")
config.write_text("theme=dark\nversion=1.0", encoding="utf-8")
content = config.read_text(encoding="utf-8")
print(content)
# Write bytes
Path("data.bin").write_bytes(b"\x00\x01\x02\x03")
raw = Path("data.bin").read_bytes()
# ── Directory operations ──────────────────────────────
logs_dir = Path("logs") / "2025"
logs_dir.mkdir(parents=True, exist_ok=True) # create nested dirs
# ── Find files by pattern ─────────────────────────────
for txt_file in Path(".").glob("*.txt"):
print(txt_file) # all .txt in current dir
for py_file in Path(".").rglob("*.py"):
print(py_file) # recursive — all .py files
# ── File stats ────────────────────────────────────────
stat = config.stat()
print(stat.st_size) # file size in bytes
print(stat.st_mtime) # last modification timestamp
# ── Delete a file safely ──────────────────────────────
if config.exists():
config.unlink() # delete the file
Practice Python Online — Free Compilers
Don't want to install anything yet? Practice Python file handling examples from this chapter directly in your browser with these free Python environments — most support file I/O.
open(file, mode, encoding)opens a file. Always specifyencoding="utf-8"for text files to ensure cross-platform consistency.- File modes:
"r"read (default),"w"write+truncate,"a"append,"x"exclusive create,"b"binary modifier,"+"read+write modifier. - Reading methods:
read()(all at once),read(n)(n chars),readline()(one line),readlines()(list). Best practice:for line in f:— memory efficient. - Writing:
write(str)(no auto-newline),writelines(list), orprint(text, file=f). Remember:"w"truncates immediately! - Always use the
withstatement — it guaranteesf.close()even if an exception occurs. Open multiple files in onewithblock. - Binary mode (
"rb","wb") works withbytes— no encoding argument. Usestr.encode()andbytes.decode()to convert. - CSV: use
csv.reader/csv.writerandcsv.DictReader/csv.DictWriter. Always usenewline=""when opening CSV files. - JSON:
json.dump(data, f)writes to file;json.load(f)reads from file. Useindent=2for readable output. - pathlib.Path — modern OOP paths:
.read_text(),.write_text(),.exists(),.mkdir(),.glob(),.unlink(). Prefer it overos.path.
The with statement implements the context manager protocol (__enter__/__exit__). It
guarantees the file is closed when the block exits — even if an exception occurs
inside.
Without with, you must call f.close() manually in a finally
block to ensure cleanup. If you forget, the file remains open, which can cause resource leaks, data
corruption, or OSError: too many open files.
"w" (write) — opens the file for writing. If the file
already exists, all its content is destroyed immediately. If it doesn't exist, a new file
is created. The file pointer starts at the beginning.
"a" (append) — opens the file for
writing. Existing content is preserved. New content is added to the end. If the file
doesn't exist, it's created. Use "a" for log files.
Iterating over the file object directly with a for loop — it
reads one line at a time without loading the entire file into memory:
with open("huge.txt", encoding="utf-8") as f:
for line in f: # ✅ reads one line at a time
process(line.strip())
readlines() loads all lines into a list
first — unsafe for multi-GB files. The for line in f pattern uses
Python's lazy iteration — ideal for large files.
The csv module handles its own newline translation. If you let
Python's default text mode also translate newlines, you get double newlines (\r\n → \r\r\n) on Windows,
resulting in blank rows between every data row.
By passing newline="", you tell Python
not to do any newline translation — leaving it entirely to the csv
module, which handles it correctly across all platforms.
with open("data.csv", newline="", encoding="utf-8") as f:
reader = csv.reader(f) # ✅ correct
pathlib.Path is an object-oriented approach to
paths — instead of passing strings to functions, you call methods directly on the path object:
p / "subdir" / "file.txt"— join paths with/operator (cross-platform safe)p.read_text(),p.write_text()— read/write without explicitopen()p.exists(),p.is_file(),p.mkdir(),p.glob()— all built-in methods- More readable:
p.name,p.stem,p.suffix,p.parentas attributes
json module reads a JSON
object from an open file?.py
files in a directory and all its subdirectories?
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.