Chapter 7: File Operations / Lesson 46

Todo List Manager

🎯 Project: Todo List Manager

Let's build a todo list manager that saves tasks to a file! This project combines file handling, error handling, lists, and user interaction. You'll create a program that allows users to add, remove, and view tasks, with all data persisted to a file.

This project will help you practice file operations, error handling, and building a complete application.

Project Requirements

Your todo list manager should:

  • Load tasks from a file when program starts
  • Save tasks to a file when program ends
  • Add new tasks
  • Remove tasks
  • Display all tasks
  • Handle file errors gracefully
  • Provide a menu interface

Step-by-Step Implementation

Here's how to build the todo list manager:

todo_manager.py
# Load tasks from file def load_tasks(): tasks = [] try: with open("tasks.txt", "r") as file: for line in file: tasks.append(line.strip()) except FileNotFoundError: print("No existing tasks file. Starting fresh.") return tasks # Save tasks to file def save_tasks(tasks): with open("tasks.txt", "w") as file: for task in tasks: file.write(task + " ") # Main program tasks = load_tasks() while True: print(" Todo List Manager") print("1. Add task") print("2. Remove task") print("3. View tasks") print("4. Exit") choice = input("Enter choice: ") if choice == "1": task = input("Enter task: ") tasks.append(task) save_tasks(tasks) elif choice == "2": if tasks: for i, task in enumerate(tasks, 1): print(f"{i}. {task}") idx = int(input("Enter task number to remove: ")) - 1 if 0 <= idx < len(tasks): tasks.pop(idx) save_tasks(tasks) elif choice == "3": if tasks: for i, task in enumerate(tasks, 1): print(f"{i}. {task}") else: print("No tasks!") elif choice == "4": save_tasks(tasks) print("Goodbye!") break

Enhancement Ideas

Try adding these features:

  • Mark tasks as completed
  • Set due dates for tasks
  • Priority levels
  • Search functionality
  • Sort tasks by date or priority
🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...