Lesson 2 of 9 · 42 min · UG / diploma
Files, CSV and memory that survives
When a program stops, variables evaporate. Attendance, marks and inventory must live on disk. The simplest professional format is CSV: one header row, then rows of commas. Excel opens it. pandas reads it in one line. Databases import it. Treating a CSV as a list of dictionaries is the mental model for the rest of this level.
Persistent store
Files and tables remember after the program stops.
You will be able to
- ✓ Explain why files exist (memory vs disk)
- ✓ Model a CSV as a list of dicts
- ✓ Know open/read/write and the csv module at a professional level
RAM forgets; disks remember
A variable lives in RAM. Close the laptop, it is gone. A file is bytes on disk with a path: data/attendance.csv. Applications read at startup, write when something important changes, and never assume the previous run is still in memory.
Paths are not the same on every computer. pathlib.Path("data") / "marks.csv" joins folders correctly on Windows and Linux. Hard-coding C:\\Users\\Asha\\Desktop\\file.csv is how student projects die on the teacher’s machine.
CSV is a table in text
First line: column names. Next lines: values in the same order. Commas separate fields; quotes wrap a field that itself contains a comma. That is the whole format. It is not a database — there is no index, no type, no two-user lock — but it is the interchange format of science and government.
After you parse a CSV you almost always want a list of dicts: each row is {"name": "Meera", "present": 1}. Then filtering, grouping and writing back are the same loops you already know. pandas.read_csv does this parse for you; you should be able to picture the list of dicts it produces.
open, with, and encoding
with open(path, encoding="utf-8") as f: is the professional open. with closes the file even if you crash. encoding="utf-8" is how Indian names survive. Never open a text file without saying the encoding — Windows otherwise guesses, and गुजरात becomes garbage.
Write atomically when you can: write to marks.csv.tmp then rename. A crash mid-write should not leave a half CSV as the only copy of the term’s marks.
What not to store in CSV
Passwords, tokens, and anything you would not pin on the staff-room noticeboard. CSV has no encryption. Nested data (a student with a list of achievements) fits JSON or a database better than ten extra columns named badge1, badge2.
Very large files (millions of rows) still start as CSV in the wild. pandas and databases exist because scanning a 2 GB CSV on every page load is too slow — which is the next chapter.
Words that matter
- Persistence
- Data that still exists after the process exits.
- CSV
- Comma-separated values: a text table with a header row.
- Encoding
- How characters become bytes; use UTF-8.
- pathlib
- Standard-library paths that work across operating systems.
Common mistakes
Avoid: Parsing CSV with line.split(",") and hoping no field contains a comma.
Do this: Use the csv module or pandas.read_csv.
Avoid: Saving the only copy of marks by printing them to the screen.
Do this: Write a file (or a database) as part of the application’s job.
On a full Python install — csv + pathlib
The standard library already reads and writes CSV correctly, including quoted commas. pandas is optional sugar on top of this shape.
No pip needed.
Read attendance.csv into a list of dicts, then write a filtered file.
Real library code (not run in this browser sandbox)
import csv
from pathlib import Path
path = Path("data/attendance.csv")
rows = []
with path.open(encoding="utf-8", newline="") as f:
for row in csv.DictReader(f):
rows.append(row)
present = [r for r in rows if r["status"] == "P"]
out = Path("data/present.csv")
with out.open("w", encoding="utf-8", newline="") as f:
w = csv.DictWriter(f, fieldnames=["name", "status"])
w.writeheader()
w.writerows(present)
Example program — A CSV already loaded as rows
This is what DictReader gives you. Filter present students.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Keep the science rows
rows has subject keys. Collect names where subject is Science. Print so Anil appears.
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.