On this page

Guided Exercise: Build a Persistent To‑Do Manager

Enrico Hochkoeppler Profile

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:

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:


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:


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:


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:

Each enhancement builds on the same persistence foundation.


Summary​

In this guided exercise, you built a complete persistent to‑do manager using:

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!

Buy Me a Coffee

Previous

Serialization with Pickle

Next

Intermediate Python & Best Practices