1. Opening a file with 'w' to look at it
the data of the whole assignment, and on paper the whole questionWhat not to write
“with open('marks.txt', 'w') as f: for line in f: print(line). I only read it, so the file is unchanged.”
What to write
“with open('marks.txt', 'r') as infile: for line in infile: print(line.strip()). Mode 'w' truncates at the open, before anything is written.”
Why: Truncation is a side effect of open, not of write: a file opened with 'w' and closed at once has 0 characters left. Reading from a handle opened with 'w' also raises, but by then the content is gone.