Lesson 20: Capstone II — Save to file
The final capstone! Last lesson we built a contact book as a dict — but it vanishes when the program ends. Now we'll save it to a file so it persists. We'll combine everything: the dict from last lesson, writing to a file with with open(..., "w") from the files lesson, and reading it back. We write
A dict lives in memory and vanishes when the run ends. Saving to a file with with open(..., "w") is like writing the book into a notebook — it stays afterwards, and you can read it back.
- persistence
- Saving data to a file so it survives after the program ends, unlike a variable that vanishes.
- saving the book to a file
- Go over the dict and write each contact as a line to a file with with open(..., "w").
- loading the book from a file
- open(path).read() returns the whole file content back as a string.