Intermediate·20 min·0/47 done

Python File Handling

Read and write files with with open, work with JSON, and build paths safely.

Why it matters: Datasets arrive as JSONL, configs as JSON, and eval results go back out as files. Path bugs hide between Windows and Linux.

Basics

Open and Close with with

Always open files with with. The file closes even if an error happens.

Pass encoding="utf-8" so Windows and Linux behave the same.

Modes: "r" read, "w" write (overwrites), "a" append. Add b for bytes. Common mistake: using open() without with, then forgetting to close.

Tiny example

Write a short file, then read it back.

with open("note.txt", "w", encoding="utf-8") as f:
    f.write("hello")
with open("note.txt", "r", encoding="utf-8") as f:
    print(f.read())

Basics

JSON

json.dumps(obj) turns a Python object into a string. json.loads(s) parses it back.

For files use json.dump(obj, f) and json.load(f).

JSONL means one JSON object per line. Fine-tuning and eval datasets often use this format.

Tiny example

Dump a dict to JSON text and load it back.

import json
s = json.dumps({"model": "mini"})
print(s)
print(json.loads(s)["model"])

Basics

pathlib Paths

Use Path instead of string joins. The / operator works on every OS.

Useful methods: .exists(), .mkdir(parents=True, exist_ok=True), .read_text(), .write_text(), .glob("*.json").

Tiny example

Join a path with / and print the suffix.

from pathlib import Path
p = Path("artifacts") / "metrics.json"
print(p)
print(p.suffix)

Then AI / ML

AI / ML examples

Same idea, used in real AI work. Press Try in lab to run it.

AI / ML example

AI / ML: save a model config

Write JSON, then load it again.

In AI / ML: Model settings and eval reports are saved as JSON constantly.

example.py

Python

import json

config = {"model": "gpt-4.1-mini", "temperature": 0.2}
with open("config.json", "w", encoding="utf-8") as f:
    json.dump(config, f, indent=2)
with open("config.json", "r", encoding="utf-8") as f:
    loaded = json.load(f)
print(loaded)

AI / ML example 2

AI / ML: stream a JSONL file

One object per line, read line by line.

In AI / ML: Fine-tuning rows are almost always stored as JSONL.

example.py

Python

import json

rows = [
    {"prompt": "What is RAG?", "completion": "Retrieval plus generation."},
]
with open("train.jsonl", "w", encoding="utf-8") as f:
    for row in rows:
        f.write(json.dumps(row) + "\n")
with open("train.jsonl", "r", encoding="utf-8") as f:
    for line in f:
        print(json.loads(line))

Takeaways

  • 1Always use with open(..., encoding="utf-8").
  • 2json.dumps/loads for strings; dump/load for files.
  • 3Build paths with pathlib, not string concatenation.
Certificate