Tutorials Python Python File Handling
Ch 37 / 58
Python Beginner–Intermediate 13 min read Chapter 37 of 58

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.

What you'll learn in this chapter
open() function and all file modes explained
Reading files — read(), readline(), readlines()
Writing and appending text to files
with statement — safe file handling
CSV and JSON file reading/writing
pathlib — modern object-oriented file paths
Advertisement

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.

File lifecycle with the with statement
open("data.txt", "r") → file object
▼
with block enters
__enter__ → returns file handle (as f)
▼
your code runs here
f.read() / f.write() / f.readline() / …
▼
always called — even on exception
__exit__ → f.close() guaranteed
All File Modes — open(file, mode)
"r"
Read (default)
Opens file for reading. Raises FileNotFoundError if file doesn't exist. File pointer at start.
"w"
Write — truncates!
Creates file if missing, overwrites all existing content. Use with caution.
"a"
Append
Creates file if missing, adds content to the end. Existing content preserved. File pointer at end.
"x"
Exclusive Create
Creates new file. Raises FileExistsError if file already exists. Safe creation.
"b"
Binary modifier
Combine with r/w/a: "rb", "wb". Returns bytes instead of str. For images, PDFs, etc.
"+"
Read + Write modifier
Combine: "r+" (read+write, no truncate), "w+" (write+read, truncates).
Python — open_basics.py
# 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.

Python — reading_files.py
# ── 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.

Python — writing_files.py
# ── 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" mode truncates immediately! Opening an existing file with "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+".
Advertisement
K2infocom Python Masterclass & Backend Development
Master Python, Django & Backend APIs with AI
Go from absolute beginner to building production-ready REST APIs, web applications, and ML pipelines. Real projects, live mentoring in Vadodara, and placement support included.
2,400+
Students Enrolled
4.9 ⭐
Google Rating
6 Months
Full Program
100%
Placement Support
Python 3 Django & DRF FastAPI PostgreSQL Machine Learning Docker & AWS Real Projects AI Tools

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.

Python — with_statement.py
# ✅ 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.

Python — binary_files.py
# ── 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.

Python — csv_and_json.py
# ── 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.

Python — pathlib_file_handling.py
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.

▶
Online Python 3 Compiler
Write, run and debug Python code in your browser. No installation required. Test file handling examples instantly.
Open Compiler
Replit
Full IDE online
Google Colab
Free GPU notebooks
JupyterLite
Notebooks in browser
Programiz
Beginner-friendly
Chapter Summary
  • open(file, mode, encoding) opens a file. Always specify encoding="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), or print(text, file=f). Remember: "w" truncates immediately!
  • Always use the with statement — it guarantees f.close() even if an exception occurs. Open multiple files in one with block.
  • Binary mode ("rb", "wb") works with bytes — no encoding argument. Use str.encode() and bytes.decode() to convert.
  • CSV: use csv.reader/csv.writer and csv.DictReader/csv.DictWriter. Always use newline="" when opening CSV files.
  • JSON: json.dump(data, f) writes to file; json.load(f) reads from file. Use indent=2 for readable output.
  • pathlib.Path — modern OOP paths: .read_text(), .write_text(), .exists(), .mkdir(), .glob(), .unlink(). Prefer it over os.path.
Free Resource
Download our Python File Handling Cheat Sheet
open(), file modes, read/write, with statement, CSV, JSON & pathlib — one printable page.
Get Sheet
Advertisement
Quick Check — Test Your Understanding
Question & Answer
✓ Answer

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.

✓ Answer

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

✓ Answer

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.

✓ Answer

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
✓ Answer

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 explicit open()
  • p.exists(), p.is_file(), p.mkdir(), p.glob() — all built-in methods
  • More readable: p.name, p.stem, p.suffix, p.parent as attributes
MCQ 1 of 3
Which file mode opens a file for reading and raises an error if it does not exist?
MCQ 2 of 3
Which function from the json module reads a JSON object from an open file?
MCQ 3 of 3
Which pathlib method recursively finds all .py files in a directory and all its subdirectories?

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel