App 2: To-Do List Manager / Build the to-do app
Lesson 7 of 10
Saving to JSON
Right now tasks vanish when the app closes. json.dump writes the list to a file; call save() after every change.
Key points
- import json
- json.dump(data, file, indent=2)
- Save after every change
import json
from pathlib import Path
DATA = Path("tasks.json")
def save():
DATA.write_text(json.dumps(tasks, indent=2))Your turn
Add save() and call it at the end of add, done and delete.
Quiz · +10 XP
What does indent=2 do in json.dumps?
Log in to save progress and earn XP.