How to Create a To-Do List in Python with File Storage


A Simple Project to Organize Your Tasks & Learn File Handling

Want to build something useful while learning Python? A To-Do List app is a perfect beginner project that teaches you how to work with user input, file handling, and basic Python logic.

In this blog, you'll learn how to create a To-Do List app in Python that saves your tasks to a file — so your data stays even after the program is closed.


🧠 What You'll Learn:

  • Taking user input in Python

  • Creating, reading, and writing to text files

  • Working with simple menus and loops


🧰 Tools Needed:

  • Python 3 installed

  • Any text editor or IDE (VS Code, PyCharm, or even Notepad)


📁 Step 1: Create Your Python File

Create a new Python file called:
todo_app.py


🧱 Step 2: Add the Code

python
def load_tasks(filename="tasks.txt"): try: with open(filename, "r") as file: return [line.strip() for line in file.readlines()] except FileNotFoundError: return [] def save_tasks(tasks, filename="tasks.txt"): with open(filename, "w") as file: for task in tasks: file.write(task + "\n") def display_tasks(tasks): print("\n📋 Your To-Do List:") if not tasks: print("No tasks yet!") else: for i, task in enumerate(tasks, start=1): print(f"{i}. {task}") def main(): tasks = load_tasks() while True: print("\n--- To-Do List Menu ---") print("1. View tasks") print("2. Add a task") print("3. Remove a task") print("4. Exit") choice = input("Enter your choice (1-4): ") if choice == "1": display_tasks(tasks) elif choice == "2": task = input("Enter the new task: ") tasks.append(task) save_tasks(tasks) print("✅ Task added.") elif choice == "3": display_tasks(tasks) try: index = int(input("Enter the task number to remove: ")) - 1 if 0 <= index < len(tasks): removed = tasks.pop(index) save_tasks(tasks) print(f"❌ Removed: {removed}") else: print("Invalid number.") except ValueError: print("Please enter a valid number.") elif choice == "4": print("👋 Goodbye!") break else: print("Invalid choice. Please try again.") if __name__ == "__main__": main()

🧪 How It Works:

  • The app reads from and writes to a file named tasks.txt.

  • Tasks are stored line by line, and the app keeps them updated.

  • The menu lets users view, add, or remove tasks.


💾 Why File Storage Matters

Most beginners make to-do lists that only work until the script ends.
But with file storage, your data persists — just like a real app.

📌 Every time you add or remove a task, it updates the tasks.txt file!


🚀 Bonus: Ideas to Upgrade This App

  • ✅ Mark tasks as completed

  • 🗃️ Add categories or deadlines

  • 💻 Build a GUI version using Tkinter

  • 🌐 Convert it into a web app using Flask


Conclusion

This project is a fantastic way to practice:

  • Python basics

  • File handling

  • Loops & functions

  • User input handling

The best part? It’s useful in real life!


💬 Want a GUI version or an advanced to-do list with priorities? Drop your comment below, and I’ll help you build it!

Post a Comment

Post a Comment (0)

Previous Post Next Post