HomeCourseModule 06 › Files and paths — the pathlib essentials

Files and paths — the pathlib essentials

Module 06 · Data Structures8 min readBeginner

What you'll learn

  • Read and write text files
  • Use pathlib to build cross-platform paths
  • Loop through every file in a folder

Reading a text file

with open("notes.txt", "r") as f:
    contents = f.read()
print(contents)

with open(...) as f: is the safe pattern — Python closes the file for you automatically when the indented block ends.

Writing a text file

with open("output.txt", "w") as f:
    f.write("Hello!\n")
    f.write("Second line.\n")

"w" overwrites; "a" appends.

Reading line by line

with open("notes.txt") as f:
    for line in f:
        print(line.strip())

pathlib — the modern way to build paths

from pathlib import Path

home = Path.home()                     # ~/
desktop = home / "Desktop"             # ~/Desktop
output = desktop / "report.xlsx"       # ~/Desktop/report.xlsx

output.exists()           # True/False
output.name               # 'report.xlsx'
output.suffix             # '.xlsx'
output.parent             # PosixPath('~/Desktop')

The / operator joins paths in the correct way for Mac/Linux/Windows. Use this everywhere instead of string concatenation.

Looping through every file in a folder

from pathlib import Path

folder = Path("reports/2026/q1")
for path in folder.glob("*.xlsx"):
    print(path.name, path.stat().st_size)

glob("*.xlsx") = "every .xlsx in this folder."
rglob("*.xlsx") = "every .xlsx in this folder and all subfolders."

Walkthrough: gather every CSV from a year's worth of monthly subfolders

The structure

data/
  2026-01/
    sales.csv
    returns.csv
  2026-02/
    sales.csv
  ...

List them all

from pathlib import Path

for p in Path("data").rglob("*.csv"):
    print(p)

Read and combine

import pandas as pd
frames = [pd.read_csv(p) for p in Path("data").rglob("sales.csv")]
combined = pd.concat(frames, ignore_index=True)
print(len(combined))

Six lines of code, dozens of files, one DataFrame. This pattern alone has saved many an analyst's morning.

Key takeaways

  • with open(...) as f: is the safe way to read/write text files.
  • pathlib.Path + / builds cross-platform paths.
  • glob("*.csv") and rglob("*.csv") iterate every file matching a pattern.
  • Combine with pd.concat() to merge many files into one DataFrame.

Inventory script

Write a script that lists every file in your Downloads folder along with its size. Print a one-line total at the end.

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.