Module 8: Files
Read and write text, CSV and JSON files so your programs remember things.
- Reading files: Open files with with open(path) as f:; read() gives one string, readline() one line (with \n, "" at the end), readlines() a list; strip each line.
- Writing files: "w" wipes, "a" appends, "r+" overwrites in place; write adds no newline and returns the count; tell() is where you are, seek(n) jumps there.
- Text-file programs: Text-file programs: read, then loop over characters, words (split()) or lines; strip punctuation and lowercase before comparing; open the output file once.
- CSV and JSON: csv.reader/csv.writer (with newline="") for tables, values come back as strings; json.dump/json.load save dicts and lists as they are (keys become strings).
Ends with a 13-question checkpoint (80% passes) and a mini project: Canteen Counter v1: daily sales and the day-end report.