App 2: To-Do List Manager / Build the to-do app
Lesson 8 of 10
Loading on start
When the app starts, load tasks.json if it exists. Wrap it in try/except so a broken file doesn't crash the app.
Key points
- Check the file exists
- json.loads turns text into Python
- Catch json.JSONDecodeError
def load():
if DATA.exists():
try:
return json.loads(DATA.read_text())
except json.JSONDecodeError:
print("Couldn't read tasks.json, starting fresh.")
return []
tasks = load()Your turn
Add load(), add some tasks, quit, and run the app again. Your tasks should still be there.
Quiz · +10 XP
Why wrap json.loads in try/except?
Log in to save progress and earn XP.