Lesson 2 of 21 · 52 min · UG / professional
Files: open, read, write, close
A CSV on disk is text until you parse it. open, read, write, close (or with) are the edge. In memory the honest twin is a list of labelled rows you already know how to filter.
Sports register
min, max and mean from a table.
You will be able to
- ✓ See open, read, write and close (or with) as the laptop edge around a file
- ✓ Treat a CSV in memory as a list of labelled rows
- ✓ Filter a table without destroying the only copy of this morning’s export
A file is text until you parse it
The attendance CSV on the lab PC is bytes on disk. open opens a handle, read pulls text, write sends text back, close (or with) releases the handle so Windows does not keep a lock. None of that runs in this browser box. You still need the idea: the disk is the edge; the program’s honest middle is a list of dictionaries — each row already split into roll, name, present.
with open(path) as f: is the laptop spelling that closes even if a later line fails. A forgotten close is how a nightly job leaves a 0-byte file for the clerk. Practise the table here; copy the open sample onto a laptop inside a folder you control.
Laptop: with closes the handle
path = "attendance.csv"
with open(path, encoding="utf-8") as f:
text = f.read()
print(len(text))
with open("keepers.csv", "w", encoding="utf-8") as out:
out.write("roll,ok\n12,1\n")
The in-memory twin is a list of rows
Header plus rows becomes keys on each dict. You already know how to read one dict. A table is many of them. Filter by building a new list of keepers with append. Leave the raw list alone — it is the only copy of this morning’s file until a laptop write replaces the disk.
“Save” in this sandbox is a status print: Wrote, plus the keepers you would have sent. Do not pretend print is a USB stick. Do remember that a function should return the table; the edge decides the path.
Words that matter
- Handle
- The open file object — close it, or use with.
- Row
- One labelled record; a dict in this course, a CSV line on disk.
- Table
- A list of rows — the in-memory shape of a small CSV.
Common mistakes
Avoid: Erasing the only raw list while “saving” a filter.
Do this: Keep source rows; append keepers; write only at the laptop edge.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — a loaded table
This is what you would have after read + parse. First vendor is Campus Stores.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — keepers, then a write-shaped status
Source length stays 3. Keep GST-ready amounts at or above 100. Print Wrote.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
On a full Python install — open / pathlib
Disk I/O lives at the edge. Parse into rows, then the rest of the program never sees a path.
stdlib — no pip
Read, parse mentally, write keepers.
Real library code (not run in this browser sandbox)
from pathlib import Path
raw = Path("upi.csv").read_text(encoding="utf-8")
lines = raw.splitlines()
# parse header + rows on a laptop, then:
# Path("keepers.csv").write_text(out, encoding="utf-8")
print("chars", len(raw))
Example program — Present students as a new table
Source stays. present is what you would write back on a laptop.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Mark the load
rows already “arrived”. Print Loaded and the first roll 12 (not the whole list).
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.