
This classic Python To-Do List Project is one that combines all your fundamental skills: loops, functions, lists, and file handling.
We’ll build a simple program that lets a user manage a tasks.txt file.
Step 1: The Main Loop
We need a while True loop to keep the program running until the user types “quit”.
def main():
while True:
print("\n--- To-Do List ---")
print("1. View Tasks")
print("2. Add Task")
print("3. Remove Task")
print("4. Quit")
choice = input("Enter choice: ")
if choice == '1':
view_tasks()
elif choice == '2':
add_task()
elif choice == '3':
remove_task()
elif choice == '4':
break
else:
print("Invalid choice.")
if __name__ == "__main__":
main()Step 2: Add and View Tasks
We need to read from and write to our file.
# (Place this above main())
TASKS_FILE = "tasks.txt"
def view_tasks():
try:
with open(TASKS_FILE, "r") as f:
tasks = f.readlines()
if not tasks:
print("No tasks found.")
for i, task in enumerate(tasks):
print(f"{i+1}. {task.strip()}")
except FileNotFoundError:
print("No tasks found.")
def add_task():
task = input("Enter new task: ")
# Use 'a' (append) mode
with open(TASKS_FILE, "a") as f:
f.write(task + "\n")
print("Task added.")Step 3: Remove Task
This is the trickiest part. We have to read all lines into a list, remove one, and then re-write the entire file.
def remove_task():
view_tasks()
try:
task_num = int(input("Enter task number to remove: "))
with open(TASKS_FILE, "r") as f:
tasks = f.readlines()
if 1 <= task_num <= len(tasks):
# Remove the task (task_num is 1-based, list is 0-based)
tasks.pop(task_num - 1)
# Write the NEW list back to the file
with open(TASKS_FILE, "w") as f:
f.writelines(tasks)
print("Task removed.")
else:
print("Invalid task number.")
except (ValueError, FileNotFoundError):
print("Invalid input.")

![3D illustration of a file path blocked by illegal characters causing an OSError [Errno 22] Invalid Argument in Python.](https://pythonprohub.com/wp-content/uploads/2026/01/fix-oserror-errno-22-invalid-argument-file-paths-768x429.png)


