Skip to content

Todo List Application

Abstract

This is a simple Todo List Application using Python. In this project, you can add, delete, and view the list of tasks. This is a command line based application and uses the txt file to store the tasks. For the features, you can add a task, delete a task, and view the list of tasks. The application will store the tasks in the txt file and will read the tasks from the txt file. The application will also check if the txt file exists or not. If the txt file does not exist, then the application will create a new txt file and store the tasks in it. The application will also check if the task is already present in the txt file or not. If the task is already present in the txt file, then the application will not add the task to the txt file. The application will also check if the task is present in the txt file or not. If the task is not present in the txt file, then the application will not delete the task from the txt file.

Prerequisites

  • Python 3.6 or above
  • Text Editor or IDE

Getting Started

Creating a new project

  1. Create a new project folder and name it as todotodo.
  2. Create a new file and name it as todo.pytodo.py.
  3. Create a new file and name it as todo.txttodo.txt.
  4. Open the project folder in the text editor or IDE.
  5. Copy the code from todo.pytodo.py and paste it into todo.pytodo.py file.

Write the code

  1. Add the following code to the todo.pytodo.py file.
⚙️ Todo List App
Todo List App
# 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 App
# 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. Open the terminal and navigate to the project folder.
  3. Run the following command to run the application.
command
C:\Users\username\Documents\todo> python todo.py
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 1
Add Task
---------
Enter Task: Going for the google summit
Task Added Successfully.
Enter Choice (1/2/3/4/E/R/H): 2
View Task
---------
1. Going for the google summit
Enter Choice (1/2/3/4/E/R/H): 3
Delete Task
------------
1. Going for the google summit
Enter Task Number to Delete: 1
Task Deleted Successfully.
Enter Choice (1/2/3/4/E/R/H): 2
View Task
---------
No Tasks Found.
Enter Choice (1/2/3/4/E/R/H): H
Help
----
Add Task: Add a task to the todo list.
View Task: View all tasks in the todo list.
Delete Task: Delete a task from the todo list.
Delete All Task: Delete all tasks from the todo list.
Exit: Exit the application.
Restart: Restart the application.
Help: View help.
Enter Choice (1/2/3/4/E/R/H): E
Exit
-----
Are you sure you want to exit? (Y/N): Y
Exiting...
command
C:\Users\username\Documents\todo> python todo.py
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 1
Add Task
---------
Enter Task: Going for the google summit
Task Added Successfully.
Enter Choice (1/2/3/4/E/R/H): 2
View Task
---------
1. Going for the google summit
Enter Choice (1/2/3/4/E/R/H): 3
Delete Task
------------
1. Going for the google summit
Enter Task Number to Delete: 1
Task Deleted Successfully.
Enter Choice (1/2/3/4/E/R/H): 2
View Task
---------
No Tasks Found.
Enter Choice (1/2/3/4/E/R/H): H
Help
----
Add Task: Add a task to the todo list.
View Task: View all tasks in the todo list.
Delete Task: Delete a task from the todo list.
Delete All Task: Delete all tasks from the todo list.
Exit: Exit the application.
Restart: Restart the application.
Help: View help.
Enter Choice (1/2/3/4/E/R/H): E
Exit
-----
Are you sure you want to exit? (Y/N): Y
Exiting...
  1. You can view the tasks in the todo.txttodo.txt file.
todo.txt
Going for the google summit
todo.txt
Going for the google summit

Explanation

  1. The import osimport os statement will import the osos module.
  2. The import timeimport time statement will import the timetime module.
  3. The import sysimport sys statement will import the syssys module.
  4. The import datetimeimport datetime statement will import the datetimedatetime module.
  5. The def add_task():def add_task(): statement will define a function named add_taskadd_task. It will add a task to the todo list. It will take no parameters.
  6. add_task()add_task() function write the task to the todo.txttodo.txt file using the with open("todo.txt", "a") as f:with open("todo.txt", "a") as f: statement. It append the task at the end of the file.
  7. The def view_task():def view_task(): statement will define a function named view_taskview_task. It will view all tasks in the todo list. It will take no parameters.
  8. view_task()view_task() function read the tasks from the todo.txttodo.txt file using the with open("todo.txt", "r") as f:with open("todo.txt", "r") as f: statement.
  9. The def delete_task():def delete_task(): statement will define a function named delete_taskdelete_task. It will delete a task from the todo list. It will take no parameters.
  10. delete_task()delete_task() function read the tasks from the todo.txttodo.txt file using the with open("todo.txt", "r") as f:with open("todo.txt", "r") as f: statement. It will ask the user to enter the task number to delete. It will delete the task from the todo.txttodo.txt file using the del tasks[task_no - 1]del tasks[task_no - 1] statement.
  11. The def delete_all_task():def delete_all_task(): statement will define a function named delete_all_taskdelete_all_task. It will delete all tasks from the todo list. It will take no parameters.
  12. delete_all_task()delete_all_task() function read the tasks from the todo.txttodo.txt file using the with open("todo.txt", "r") as f:with open("todo.txt", "r") as f: statement. It will ask the user to confirm the deletion of all tasks. It will delete all tasks from the todo.txttodo.txt file using the with open("todo.txt", "w") as f:with open("todo.txt", "w") as f: statement.
  13. The def exit():def exit(): statement will define a function named exitexit. It will exit the application. It will take no parameters. It will ask the user to confirm the exit. It will exit the application using the sys.exit()sys.exit() statement.
  14. The def restart():def restart(): statement will define a function named restartrestart. It will restart the application. It will take no parameters. It will ask the user to confirm the restart. It will restart the application using the os.system("python todo.py")os.system("python todo.py") statement.
  15. The def help():def help(): statement will define a function named helphelp. It will display the help. It will take no parameters. It will display the help using the print()print() statement. It helps the user to use the application.
  16. The print("Todo Application")print("Todo Application") statement will display the Todo Application text on the screen.
  17. We are going to use whilewhile loop to run the application continuously. The while True:while True: statement will create an infinite loop.
  18. When the user wants to exit the application, then the sys.exit()sys.exit() statement will exit the application.

Usage

  1. Open the terminal and navigate to the project folder.
  2. Run the following command to run the application.
command
C:\Users\username\Documents\hello_world> python todo.py
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 
command
C:\Users\username\Documents\hello_world> python todo.py
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 
  1. Enter the choice and press the enter key.
command
C:\Users\username\Documents\hello_world> python todo.py
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 1
command
C:\Users\username\Documents\hello_world> python todo.py
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 1
  1. If you enter 11 and press the enter key, then the application will add a task to the todo list.
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 1
Add Task
---------
Enter Task: Going for the google summit
Task Added Successfully.
Enter Choice (1/2/3/4/E/R/H): 
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 1
Add Task
---------
Enter Task: Going for the google summit
Task Added Successfully.
Enter Choice (1/2/3/4/E/R/H): 
  1. If you enter 22 and press the enter key, then the application will view all tasks in the todo list.
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 2
View Task
---------
1. Going for the google summit
Enter Choice (1/2/3/4/E/R/H): 
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 2
View Task
---------
1. Going for the google summit
Enter Choice (1/2/3/4/E/R/H): 
  1. If you enter 33 and press the enter key, then the application will delete a task from the todo list. Application shows the list of tasks and asks the user to enter the task number to delete.
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 3
Delete Task
------------
1. Going for the google summit
Enter Task Number to Delete: 1
Task Deleted Successfully.
Enter Choice (1/2/3/4/E/R/H): 
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 3
Delete Task
------------
1. Going for the google summit
Enter Task Number to Delete: 1
Task Deleted Successfully.
Enter Choice (1/2/3/4/E/R/H): 
  1. If you enter 44 and press the enter key, then the application will delete all tasks from the todo list. Application shows the list of tasks and asks the user to confirm the deletion of all tasks.
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 4
Delete All Task
----------------
1. Going for the google summit
Are you sure you want to delete all tasks? (Y/N): Y
All Tasks Deleted Successfully.
Enter Choice (1/2/3/4/E/R/H): 
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 4
Delete All Task
----------------
1. Going for the google summit
Are you sure you want to delete all tasks? (Y/N): Y
All Tasks Deleted Successfully.
Enter Choice (1/2/3/4/E/R/H): 
  1. If you enter EE or ee and press the enter key, then the application will exit.
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): E
Exit
-----
Are you sure you want to exit? (Y/N): Y
Exiting...
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): E
Exit
-----
Are you sure you want to exit? (Y/N): Y
Exiting...
  1. If you enter RR or rr and press the enter key, then the application will restart.
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): R
Restart
--------
Are you sure you want to restart? (Y/N): Y
Restarting...
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): R
Restart
--------
Are you sure you want to restart? (Y/N): Y
Restarting...
  1. If you enter HH or hh and press the enter key, then the application will display the help.
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): H
Help
----
Add Task: Add a task to the todo list.
View Task: View all tasks in the todo list.
Delete Task: Delete a task from the todo list.
Delete All Task: Delete all tasks from the todo list.
Exit: Exit the application.
Restart: Restart the application.
Help: View help.
Enter Choice (1/2/3/4/E/R/H): 
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): H
Help
----
Add Task: Add a task to the todo list.
View Task: View all tasks in the todo list.
Delete Task: Delete a task from the todo list.
Delete All Task: Delete all tasks from the todo list.
Exit: Exit the application.
Restart: Restart the application.
Help: View help.
Enter Choice (1/2/3/4/E/R/H): 
  1. If you enter any other choice and press the enter key, then the application will display the invalid choice message.
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 5
Invalid Choice.
Enter Choice (1/2/3/4/E/R/H): 
command
Todo Application
----------------
Select Operation.
1. Add Task
2. View Task
3. Delete Task
4. Delete All Task
E. Exit
R. Restart
H. Help
Enter Choice (1/2/3/4/E/R/H): 5
Invalid Choice.
Enter Choice (1/2/3/4/E/R/H): 

Next Steps

Congratulations you have successfully created a Todo List Application using Python. Experiment with the code and try to modify it.

Here are a few suggestions:

  1. Add a feature to edit a task.
  2. Add a feature to sort the tasks.
  3. Add a feature to search for a task.
  4. Add a feature to add a task to a specific position.
  5. Add a feature to add a task to a specific date.
  6. Add a feature to add a task to a specific time.
  7. Add a feature to add a task to a specific date and time.
  8. Add a feature to add a task to a specific date and time and set a reminder.
  9. Add a feature to add a task to a specific date and time and set a reminder and repeat the task.
  10. Add a feature to add a task to a specific date and time and set a reminder and repeat the task and set a priority.

Conclusion

In this project, You learned how to build a Todo List Application using Python. You also learned how to add a task, view a task, delete a task, and delete all tasks.

You also learned how to use the osos module, timetime module, syssys module, and datetimedatetime module. You also learned how to use the withwith statement. You also learned how to use the whilewhile loop. You also learned how to use the ifif statement. You also learned how to use the elifelif statement. You also learned how to use the elseelse statement. You also learned how to use the defdef statement. You also learned how to use the print()print() function. You also learned how to use the input()input() function. You also learned how to use the open()open() function. You also learned how to use the deldel statement. You also learned how to use the sys.exit()sys.exit() function. You can find more information about the osos module, timetime module, syssys module, and datetimedatetime module on the official Python documentation website.

Keep experimenting with the code and try to modify it. Find more tutorials on the Python Central Hub.

Was this page helpful?

Let us know how we did