Skip to content

Todo List Application

Abstract

Create a fully functional todo list application that allows users to add, view, delete, and manage tasks through a command-line interface. The application features persistent file storage, comprehensive task management operations, and user-friendly confirmation dialogs.

Prerequisites

  • Python 3.6 or above
  • Text Editor or IDE
  • Basic understanding of Python syntax
  • Knowledge of file operations (read, write, append)
  • Familiarity with lists and string manipulation
  • Understanding of control structures and loops

Getting Started

Create a new project

  1. Create a new project folder and name it todoListApptodoListApp.
  2. Create a new file and name it todo.pytodo.py.
  3. Open the project folder in your favorite text editor or IDE.
  4. Copy the code below and paste it into your todo.pytodo.py file.

Write the code

  1. Add the following code to your todo.pytodo.py file.
⚙️ Todo List Application
Todo List Application
# Todo Application
 
# Importing Modules
import os
import time
import sys
import datetime
 
 
# Defining Functions
def add_task():
    print("Add Task")
    print("---------")
    task = input("Enter Task: ")
    with open("todo.txt", "a") as f:
        f.write(task + "\n")
    print("Task Added Successfully.")
    
def view_task():
    print("View Task")
    print("---------")
    with open("todo.txt", "r") as f:
        tasks = f.readlines()
        if len(tasks) == 0:
            print("No Tasks Found.")
        else:
            for i in range(len(tasks)):
                print(str(i + 1) + ". " + tasks[i].strip("\n"))
                
def delete_task():
    print("Delete Task")
    print("------------")
    with open("todo.txt", "r") as f:
        tasks = f.readlines()
        if len(tasks) == 0:
            print("No Tasks Found.")
        else:
            for i in range(len(tasks)):
                print(str(i + 1) + ". " + tasks[i].strip("\n"))
            task_no = int(input("Enter Task Number to Delete: "))
            if task_no > len(tasks):
                print("Invalid Task Number.")
            else:
                del tasks[task_no - 1]
                with open("todo.txt", "w") as f:
                    for task in tasks:
                        f.write(task)
                print("Task Deleted Successfully.")
                
def delete_all_task():
    print("Delete All Task")
    print("----------------")
    with open("todo.txt", "r") as f:
        tasks = f.readlines()
        if len(tasks) == 0:
            print("No Tasks Found.")
        else:
            for i in range(len(tasks)):
                print(str(i + 1) + ". " + tasks[i].strip("\n"))
            confirm = input("Are you sure you want to delete all tasks? (Y/N): ")
            if confirm in ("Y", "y"):
                with open("todo.txt", "w") as f:
                    f.write("")
                print("All Tasks Deleted Successfully.")
            elif confirm in ("N", "n"):
                print("No Tasks Deleted.")
            else:
                print("Invalid Choice.")
                
def exit():
    print("Exit")
    print("-----")
    confirm = input("Are you sure you want to exit? (Y/N): ")
    if confirm in ("Y", "y"):
        print("Exiting...")
        time.sleep(1)
        sys.exit()
    elif confirm in ("N", "n"):
        print("Not Exiting.")
    else:
        print("Invalid Choice.")
        
def restart():
    print("Restart")
    print("--------")
    confirm = input("Are you sure you want to restart? (Y/N): ")
    if confirm in ("Y", "y"):
        print("Restarting...")
        time.sleep(1)
        os.system("python todo.py")
    elif confirm in ("N", "n"):
        print("Not Restarting.")
    else:
        print("Invalid Choice.")
        
def help():
    print("Help")
    print("----")
    print("Add Task: Add a task to the todo list.")
    print("View Task: View all tasks in the todo list.")
    print("Delete Task: Delete a task from the todo list.")
    print("Delete All Task: Delete all tasks from the todo list.")
    print("Exit: Exit the application.")
    print("Restart: Restart the application.")
    print("Help: View help.")
    
 
# Main Program
print("Todo Application")
print("----------------")
print("Select Operation.")
print("1. Add Task")
print("2. View Task")
print("3. Delete Task")
print("4. Delete All Task")
print("E. Exit")
print("R. Restart")
print("H. Help")
 
while True:
    choice = input("Enter Choice (1/2/3/4/E/R/H): ")
    
    if choice == "1":
        add_task()
    elif choice == "2":
        view_task()
    elif choice == "3":
        delete_task()
    elif choice == "4":
        delete_all_task()
    elif choice.upper() == "E":
        exit()
    elif choice.upper() == "R":
        restart()
    elif choice.upper() == "H":
        help()
    else:
        print("Invalid Choice.") 
Todo List Application
# Todo Application
 
# Importing Modules
import os
import time
import sys
import datetime
 
 
# Defining Functions
def add_task():
    print("Add Task")
    print("---------")
    task = input("Enter Task: ")
    with open("todo.txt", "a") as f:
        f.write(task + "\n")
    print("Task Added Successfully.")
    
def view_task():
    print("View Task")
    print("---------")
    with open("todo.txt", "r") as f:
        tasks = f.readlines()
        if len(tasks) == 0:
            print("No Tasks Found.")
        else:
            for i in range(len(tasks)):
                print(str(i + 1) + ". " + tasks[i].strip("\n"))
                
def delete_task():
    print("Delete Task")
    print("------------")
    with open("todo.txt", "r") as f:
        tasks = f.readlines()
        if len(tasks) == 0:
            print("No Tasks Found.")
        else:
            for i in range(len(tasks)):
                print(str(i + 1) + ". " + tasks[i].strip("\n"))
            task_no = int(input("Enter Task Number to Delete: "))
            if task_no > len(tasks):
                print("Invalid Task Number.")
            else:
                del tasks[task_no - 1]
                with open("todo.txt", "w") as f:
                    for task in tasks:
                        f.write(task)
                print("Task Deleted Successfully.")
                
def delete_all_task():
    print("Delete All Task")
    print("----------------")
    with open("todo.txt", "r") as f:
        tasks = f.readlines()
        if len(tasks) == 0:
            print("No Tasks Found.")
        else:
            for i in range(len(tasks)):
                print(str(i + 1) + ". " + tasks[i].strip("\n"))
            confirm = input("Are you sure you want to delete all tasks? (Y/N): ")
            if confirm in ("Y", "y"):
                with open("todo.txt", "w") as f:
                    f.write("")
                print("All Tasks Deleted Successfully.")
            elif confirm in ("N", "n"):
                print("No Tasks Deleted.")
            else:
                print("Invalid Choice.")
                
def exit():
    print("Exit")
    print("-----")
    confirm = input("Are you sure you want to exit? (Y/N): ")
    if confirm in ("Y", "y"):
        print("Exiting...")
        time.sleep(1)
        sys.exit()
    elif confirm in ("N", "n"):
        print("Not Exiting.")
    else:
        print("Invalid Choice.")
        
def restart():
    print("Restart")
    print("--------")
    confirm = input("Are you sure you want to restart? (Y/N): ")
    if confirm in ("Y", "y"):
        print("Restarting...")
        time.sleep(1)
        os.system("python todo.py")
    elif confirm in ("N", "n"):
        print("Not Restarting.")
    else:
        print("Invalid Choice.")
        
def help():
    print("Help")
    print("----")
    print("Add Task: Add a task to the todo list.")
    print("View Task: View all tasks in the todo list.")
    print("Delete Task: Delete a task from the todo list.")
    print("Delete All Task: Delete all tasks from the todo list.")
    print("Exit: Exit the application.")
    print("Restart: Restart the application.")
    print("Help: View help.")
    
 
# Main Program
print("Todo Application")
print("----------------")
print("Select Operation.")
print("1. Add Task")
print("2. View Task")
print("3. Delete Task")
print("4. Delete All Task")
print("E. Exit")
print("R. Restart")
print("H. Help")
 
while True:
    choice = input("Enter Choice (1/2/3/4/E/R/H): ")
    
    if choice == "1":
        add_task()
    elif choice == "2":
        view_task()
    elif choice == "3":
        delete_task()
    elif choice == "4":
        delete_all_task()
    elif choice.upper() == "E":
        exit()
    elif choice.upper() == "R":
        restart()
    elif choice.upper() == "H":
        help()
    else:
        print("Invalid Choice.") 
  1. Save the file.
  2. Run the following command to run the application.
command
C:\Users\username\Documents\todoListApp> python todo.py
----------TO DO LIST----------
1. Add task
2. Delete task
3. Show task
4. exit
enter the choice: 1
enter the task: Buy groceries
Task Added Successfully.
 
enter the choice: 3
1: Buy groceries
 
enter the choice: 4
command
C:\Users\username\Documents\todoListApp> python todo.py
----------TO DO LIST----------
1. Add task
2. Delete task
3. Show task
4. exit
enter the choice: 1
enter the task: Buy groceries
Task Added Successfully.
 
enter the choice: 3
1: Buy groceries
 
enter the choice: 4
  1. Use the Application
    • Choose operations from the main menu (1-4, E, R, H)
    • Add tasks with descriptive names
    • View your current task list
    • Delete individual tasks or clear all tasks

Code Explanation

File-Based Persistence

todo.py
def add_task():
    task = input("Enter Task: ")
    with open("todo.txt", "a") as f:
        f.write(task + "\n")
    print("Task Added Successfully.")
todo.py
def add_task():
    task = input("Enter Task: ")
    with open("todo.txt", "a") as f:
        f.write(task + "\n")
    print("Task Added Successfully.")

Uses text file storage to maintain tasks between application sessions, ensuring data persistence.

Task Viewing with Numbering

todo.py
def view_task():
    with open("todo.txt", "r") as f:
        tasks = f.readlines()
        if len(tasks) == 0:
            print("No Tasks Found.")
        else:
            for i in range(len(tasks)):
                print(str(i + 1) + ". " + tasks[i].strip("\n"))
todo.py
def view_task():
    with open("todo.txt", "r") as f:
        tasks = f.readlines()
        if len(tasks) == 0:
            print("No Tasks Found.")
        else:
            for i in range(len(tasks)):
                print(str(i + 1) + ". " + tasks[i].strip("\n"))

Displays tasks in a numbered list format for easy reference and selection.

Safe Task Deletion

todo.py
def delete_task():
    with open("todo.txt", "r") as f:
        tasks = f.readlines()
        # Display current tasks
        task_no = int(input("Enter Task Number to Delete: "))
        if task_no > len(tasks):
            print("Invalid Task Number.")
        else:
            del tasks[task_no - 1]
            with open("todo.txt", "w") as f:
                for task in tasks:
                    f.write(task)
todo.py
def delete_task():
    with open("todo.txt", "r") as f:
        tasks = f.readlines()
        # Display current tasks
        task_no = int(input("Enter Task Number to Delete: "))
        if task_no > len(tasks):
            print("Invalid Task Number.")
        else:
            del tasks[task_no - 1]
            with open("todo.txt", "w") as f:
                for task in tasks:
                    f.write(task)

Implements safe task removal with validation and immediate file updates.

Confirmation Dialogs

todo.py
def delete_all_task():
    confirm = input("Are you sure you want to delete all tasks? (Y/N): ")
    if confirm in ("Y", "y"):
        with open("todo.txt", "w") as f:
            f.write("")
        print("All Tasks Deleted Successfully.")
todo.py
def delete_all_task():
    confirm = input("Are you sure you want to delete all tasks? (Y/N): ")
    if confirm in ("Y", "y"):
        with open("todo.txt", "w") as f:
            f.write("")
        print("All Tasks Deleted Successfully.")

Provides user confirmation for destructive operations to prevent accidental data loss.

Application Control

todo.py
def exit():
    confirm = input("Are you sure you want to exit? (Y/N): ")
    if confirm in ("Y", "y"):
        print("Exiting...")
        time.sleep(1)
        sys.exit()
todo.py
def exit():
    confirm = input("Are you sure you want to exit? (Y/N): ")
    if confirm in ("Y", "y"):
        print("Exiting...")
        time.sleep(1)
        sys.exit()

Manages application lifecycle with graceful exit confirmation.

Main Program Loop

todo.py
while True:
    choice = input("Enter Choice (1/2/3/4/E/R/H): ")
    if choice == "1":
        add_task()
    elif choice == "2":
        view_task()
    # ... other choices
todo.py
while True:
    choice = input("Enter Choice (1/2/3/4/E/R/H): ")
    if choice == "1":
        add_task()
    elif choice == "2":
        view_task()
    # ... other choices

Implements continuous operation loop with clear menu options and input validation.

Features

  • Persistent Storage: Tasks saved to file system survive application restarts
  • CRUD Operations: Complete Create, Read, Update, Delete functionality
  • User-Friendly Interface: Clear menu system with numbered options
  • Input Validation: Handles invalid choices and task numbers gracefully
  • Confirmation Dialogs: Prevents accidental deletion of tasks
  • Help System: Built-in help explaining all available operations
  • Application Control: Exit and restart functionality with confirmations

Next Steps

Enhancements

  • Add task due dates and reminders
  • Implement task priorities (high, medium, low)
  • Create task categories or tags
  • Add task completion status tracking
  • Implement search and filter functionality
  • Create backup and restore features
  • Add task editing capabilities
  • Include task creation timestamps

Learning Extensions

  • Study database integration (SQLite)
  • Explore GUI development with Tkinter
  • Learn about task scheduling with datetime
  • Practice with JSON data storage
  • Understand regular expressions for search
  • Explore web-based todo applications

Educational Value

This project teaches:

  • File I/O Operations: Reading from and writing to text files
  • Data Persistence: Maintaining application data between sessions
  • CRUD Operations: Implementing complete data management functionality
  • Input Validation: Handling user input safely and providing feedback
  • Application Architecture: Organizing code into focused functions
  • User Experience: Creating intuitive command-line interfaces
  • Error Handling: Managing edge cases and invalid operations
  • Control Flow: Managing complex program flow with menus and confirmations

Perfect for understanding data management, file operations, and user interface design in console applications.

Was this page helpful?

Let us know how we did