Skip to content
Laviyx Learn

Try

Python course menu

Python File Handling: Read, Write and Append Files (with open and pathlib)

Learn file handling in Python: open files with the with statement, read whole files or line by line, write and append, file modes, encodings, pathlib paths and folders, CSV files, and handling missing files.

Intermediate7 min readUpdated
On this page

Programs often need to keep data after they stop running: notes, settings, logs, reports. Reading and writing files is how they do it. Python makes this straightforward with the built-in open() function and the pathlib module.

Writing a file#

Python
with open("notes.txt", "w") as f:
    f.write("First line\n")
    f.write("Second line\n")
  • open(filename, mode) opens a file. Mode "w" means write.
  • with ... as f: gives you the open file as f, and closes it automatically when the block ends, even if an error occurs.
  • write() does not add new lines for you; include \n yourself.

Reading a whole file#

Python
with open("notes.txt") as f:
    content = f.read()

print(content)
Output
First line
Second line

The default mode is "r" (read).

Reading line by line#

Looping over a file gives one line at a time, which works even for files too large to fit in memory:

Python
with open("fruits.txt", "w") as f:
    f.write("apple\nbanana\ncherry\n")

with open("fruits.txt") as f:
    for line in f:
        print(line.strip())
Output
apple
banana
cherry

Each line ends with \n; strip() removes it (and any surrounding spaces).

Appending#

Python
with open("log.txt", "w") as f:
    f.write("started\n")

with open("log.txt", "a") as f:
    f.write("finished\n")

with open("log.txt") as f:
    print(f.readlines())
Output
['started\n', 'finished\n']

readlines() returns a list with one string per line.

File modes#

ModeMeaningIf the file existsIf it does not
"r"read (default)reads itFileNotFoundError
"w"writeerases itcreates it
"a"appendadds to the endcreates it
"x"createFileExistsErrorcreates it

Add "b" for binary files such as images ("rb", "wb").

Encoding#

Always pass encoding="utf-8" for text files. Without it, Python uses the operating system's default, which on Windows may not handle characters like ₹ or Hindi text:

Python
names = ["Asha", "Ravi", "Meera"]

with open("names.txt", "w", encoding="utf-8") as f:
    f.write("\n".join(names))

with open("names.txt", encoding="utf-8") as f:
    loaded = f.read().splitlines()

print(loaded)
Output
['Asha', 'Ravi', 'Meera']

Handling a missing file#

Python
open("missing.txt")
Output
FileNotFoundError: [Errno 2] No such file or directory: 'missing.txt'

Catch it with try/except (see Exceptions), or check first with Path("missing.txt").exists().

pathlib: modern paths#

The pathlib module treats paths as objects, joins them with / on every operating system, and has handy shortcuts:

Python
from pathlib import Path

p = Path("data") / "report.txt"
p.parent.mkdir(exist_ok=True)               # create the data folder
p.write_text("Total: 370\n", encoding="utf-8")

print(p.exists(), p.name, p.suffix, p.read_text(encoding="utf-8").strip())
print(sorted(x.name for x in Path(".").iterdir()))
Output
True report.txt .txt Total: 370
['data']

Path.glob("*.txt") finds files by pattern, which is very useful for automation.

CSV files#

The csv module reads and writes spreadsheet-style data correctly, including commas inside quoted values:

Python
import csv

rows = [["name", "city", "score"], ["Asha", "Pune", 92], ["Ravi", "Delhi", 85]]

with open("scores.csv", "w", newline="", encoding="utf-8") as f:
    csv.writer(f).writerows(rows)

with open("scores.csv", newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        print(row["name"], int(row["score"]) + 1)
Output
Asha 93
Ravi 86

DictReader uses the header row as keys. Every value comes back as a string, so convert numbers yourself. Pass newline="" when opening CSV files, as the csv documentation recommends.

Common mistakes#

Practice#

Practice exercise

A file sales.txt contains one sale amount per line, with some blank lines. Write a program that reads it and prints the number of sales and the total. Use this content:

sales.txt (Text)
250
120

560
90
Show hint

Loop over the file, strip() each line, skip empty ones with continue, and convert the rest with int().

Show solution
Python
total = 0
count = 0

with open("sales.txt") as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        total += int(line)
        count += 1

print(f"{count} sales, total {total}")
Output
4 sales, total 1020

Without the blank-line check, int("") would raise a ValueError.

Key takeaway#

Finished this lesson?