How to Create a Simple To-Do List App in Python (with File Saving)
- Anubhav Somani
- Sep 3
- 2 min read

Simple To-Do List Application. This project is a step up because it introduces you to a crucial skill: saving data to a file so that your information isn't lost when you close the program.
First, here is the explanation of how the project is built.
How the "Simple To-Do List" is Made
The Core Idea: The goal is to create a command-line to-do list where a user can add tasks, view their current tasks, and remove completed tasks. Crucially, the list of tasks will be saved to a text file (tasks.txt), so when the user restarts the program, their previous tasks are automatically loaded.
The Data Structure: A List: A simple Python list is the perfect way to store the to-do items. Each task will be a string, and all the tasks will be held in a single list, like tasks = ["Buy milk", "Finish homework"].
File Handling (The New Skill):
Saving Tasks: To save the list, we'll create a function that opens a file (e.g., tasks.txt) in "write" mode ('w'). This mode will overwrite the file with the current list. We'll loop through our tasks list and write each task string to the file, adding a newline character (\n) after each one so they are stored on separate lines.
Loading Tasks: When the program starts, we need a function to load the tasks. This function will try to open tasks.txt in "read" mode ('r'). If the file exists, it will read each line, strip away the invisible newline character using .strip(), and add the clean task to our list. We must also handle the FileNotFoundError, which happens if the program is run for the first time and the file hasn't been created yet. In that case, we just start with an empty list.
A Menu-Driven Interface: Like the contact book, we use a while True loop to create a persistent menu. The menu will offer options:
View tasks
Add a new task
Delete a task
Exit
Organizing with Functions: We'll create separate functions for each action to keep the code clean:
view_tasks(): This function checks if the list is empty. If not, it loops through the tasks using enumerateto display them with a number, making them easy to identify for deletion.
add_task(): This function prompts the user for a new task and uses the .append() method to add it to our list.
delete_task(): This function asks the user for the number of the task they want to delete. It uses a try-except block to safely convert the input to an integer and remove the corresponding task from the list using tasks.pop(index).
Putting It All Together: The main program loop will handle the user's menu choice, calling the appropriate function. When the user chooses to "Exit," the program will first call the save_tasks() function to write the current list to the file, and then it will break the loop and end. This ensures their progress is always saved.
Lucky link - https://amzn.to/45VVivD



Comments