- Courses
- 💻Programming Languages
- 🐍Python
- File Handling & Persistence
- Guided Exercise: Build a Persistent To‑Do Manager
On this page
Guided Exercise: Build a Persistent To‑Do Manager
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
Project Overview
This guided exercise brings together everything from file handling, JSON persistence, and clean program structure.
You will build a persistent to‑do manager that:
- Stores tasks in a JSON file
- Loads tasks when the program starts
- Saves tasks automatically after changes
- Supports adding, listing, completing, and removing tasks
- Uses clean, modular functions
This is a real‑world pattern: a lightweight, file‑backed data store.
Step 1 — Define the Data File
Choose a JSON file to store tasks:
DATA_FILE = "tasks.json"
If the file doesn’t exist, the program should create it with an empty list.
Step 2 — Implement File Loading
Create a function that loads tasks safely.
import json
from pathlib import Path
DATA_FILE = Path("tasks.json")
def load_tasks():
if not DATA_FILE.exists():
return []
with DATA_FILE.open("r", encoding="utf-8") as f:
return json.load(f)
This ensures:
- The file is optional
- The program always returns a valid list
Step 3 — Implement File Saving
A helper function writes the updated task list back to disk.
def save_tasks(tasks):
with DATA_FILE.open("w", encoding="utf-8") as f:
json.dump(tasks, f, indent=2)
Using indent=2 keeps the file readable.
Step 4 — Add a Task
A task is represented as a dictionary:
def add_task(tasks, description):
task = {
"description": description,
"done": False
}
tasks.append(task)
save_tasks(tasks)
return task
This function:
- Adds a new task
- Saves immediately
- Returns the created task
Step 5 — List Tasks
Display tasks with status indicators.
def list_tasks(tasks):
for i, task in enumerate(tasks, start=1):
status = "✓" if task["done"] else " "
print(f"{i}. [{status}] {task['description']}")
This produces clean, readable output.
Step 6 — Mark a Task as Completed
Update the task and save.
def complete_task(tasks, index):
if 0 <= index < len(tasks):
tasks[index]["done"] = True
save_tasks(tasks)
return True
return False
Step 7 — Remove a Task
Delete a task by index.
def remove_task(tasks, index):
if 0 <= index < len(tasks):
tasks.pop(index)
save_tasks(tasks)
return True
return False
Step 8 — Build a Simple Command Loop
A minimal interactive interface:
def main():
tasks = load_tasks()
while True:
command = input("Command (add/list/done/remove/quit): ").strip()
if command == "add":
desc = input("Task description: ")
add_task(tasks, desc)
elif command == "list":
list_tasks(tasks)
elif command == "done":
idx = int(input("Task number: ")) - 1
complete_task(tasks, idx)
elif command == "remove":
idx = int(input("Task number: ")) - 1
remove_task(tasks, idx)
elif command == "quit":
break
This loop:
- Loads tasks once
- Saves after every change
- Keeps the JSON file always up‑to‑date
Step 9 — Example tasks.json Output
After adding a few tasks, the file might look like:
[
{
"description": "Buy groceries",
"done": false
},
{
"description": "Finish Python project",
"done": true
}
]
Readable, portable, and easy to modify.
Step 10 — Optional Enhancements
You can extend the system with:
- Due dates
- Priority levels
- Categories
- Sorting
- Command‑line arguments
- Automatic backups
- Pretty terminal UI
Each enhancement builds on the same persistence foundation.
Summary
In this guided exercise, you built a complete persistent to‑do manager using:
- JSON for structured storage
- File reading and writing
- Safe loading and saving patterns
- Modular functions
- A simple interactive interface
This project demonstrates how Python applications can maintain state across runs—an essential skill for real‑world software development.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Serialization with Pickle
Next
Intermediate Python & Best Practices
- Project Overview
- Step 1 — Define the Data File
- Step 2 — Implement File Loading
- Step 3 — Implement File Saving
- Step 4 — Add a Task
- Step 5 — List Tasks
- Step 6 — Mark a Task as Completed
- Step 7 — Remove a Task
- Step 8 — Build a Simple Command Loop
- Step 9 — Example
tasks.jsonOutput - Step 10 — Optional Enhancements
- Summary
