Course map
Stage 1: Foundations
Stage 2: Data Integration
Stage 3: Building & Shipping
Stage 4: Customer Engagement
Stage 5: Specialization

Part 02 / Topic 5 of 6

Files and paths

Customer data arrives as files more often than you would like: CSV exports, JSON dumps, fixed-width relics. The modern toolkit:

from pathlib import Path
import csv, json

export = Path("data") / "shipments.csv"           # paths compose with /
with export.open(encoding="utf-8") as f:          # always name the encoding
    rows = list(csv.DictReader(f))
report = {"rows": len(rows)}
Path("out/report.json").write_text(json.dumps(report), encoding="utf-8")

Two habits prevent real incidents:

  • Always state the encoding. A customer export saved from Excel on Windows is often cp1252, not UTF-8; the crash arrives at the first é. If a read fails, try encoding="utf-8-sig" (Excel's BOM) before anything exotic.
  • Use pathlib, not string concatenation — it handles separators and makes exists(), mkdir(parents=True) and globbing one call each.

Done here? The practice set for Part 2 — 4 exercises with solutions — is on the part page.

Search the guide