Before you run it, say out loud what it will print. Then open the answer.
What it prints
2
Put it in order
Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.
Data Flow
A partial file is worse than none. Write to a temp name, then rename, so the switch is one atomic step.
1build the new text
↓
2write a temp file
↓
3rename into place
↓
4report a failure
Line by line
Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.
rows = ["a", "b", "c"]sets nothing on screen
half = rows[:1]sets nothing on screen
print(len(half))1
Why this worksAtomic means one switch
A rename is the point where old becomes new. Everything before it can fail with no visible change.
Two states are valid
While a write runs, both the old file and the new one are readable. Readers must accept either, not assume one.