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
- Create a new project folder and name it as
todo
todo
. - Create a new file and name it as
todo.py
todo.py
. - Create a new file and name it as
todo.txt
todo.txt
. - Open the project folder in the text editor or IDE.
- Copy the code from
todo.py
todo.py
and paste it intotodo.py
todo.py
file.
Write the code
- Add the following code to the
todo.py
todo.py
file.
⚙️ 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 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.")
- Save the File.
- Open the terminal and navigate to the project folder.
- Run the following command to run the application.
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...
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...
- You can view the tasks in the
todo.txt
todo.txt
file.
Going for the google summit
Going for the google summit
Explanation
- The
import os
import os
statement will import theos
os
module. - The
import time
import time
statement will import thetime
time
module. - The
import sys
import sys
statement will import thesys
sys
module. - The
import datetime
import datetime
statement will import thedatetime
datetime
module. - The
def add_task():
def add_task():
statement will define a function namedadd_task
add_task
. It will add a task to the todo list. It will take no parameters. add_task()
add_task()
function write the task to thetodo.txt
todo.txt
file using thewith open("todo.txt", "a") as f:
with open("todo.txt", "a") as f:
statement. It append the task at the end of the file.- The
def view_task():
def view_task():
statement will define a function namedview_task
view_task
. It will view all tasks in the todo list. It will take no parameters. view_task()
view_task()
function read the tasks from thetodo.txt
todo.txt
file using thewith open("todo.txt", "r") as f:
with open("todo.txt", "r") as f:
statement.- The
def delete_task():
def delete_task():
statement will define a function nameddelete_task
delete_task
. It will delete a task from the todo list. It will take no parameters. delete_task()
delete_task()
function read the tasks from thetodo.txt
todo.txt
file using thewith 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 thetodo.txt
todo.txt
file using thedel tasks[task_no - 1]
del tasks[task_no - 1]
statement.- The
def delete_all_task():
def delete_all_task():
statement will define a function nameddelete_all_task
delete_all_task
. It will delete all tasks from the todo list. It will take no parameters. delete_all_task()
delete_all_task()
function read the tasks from thetodo.txt
todo.txt
file using thewith 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 thetodo.txt
todo.txt
file using thewith open("todo.txt", "w") as f:
with open("todo.txt", "w") as f:
statement.- The
def exit():
def exit():
statement will define a function namedexit
exit
. 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 thesys.exit()
sys.exit()
statement. - The
def restart():
def restart():
statement will define a function namedrestart
restart
. 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 theos.system("python todo.py")
os.system("python todo.py")
statement. - The
def help():
def help():
statement will define a function namedhelp
help
. It will display the help. It will take no parameters. It will display the help using theprint()
print()
statement. It helps the user to use the application. - The
print("Todo Application")
print("Todo Application")
statement will display the Todo Application text on the screen. - We are going to use
while
while
loop to run the application continuously. Thewhile True:
while True:
statement will create an infinite loop. - When the user wants to exit the application, then the
sys.exit()
sys.exit()
statement will exit the application.
Usage
- Open the terminal and navigate to the project folder.
- Run the following command to run the application.
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):
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):
- Enter the choice and press the enter key.
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
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
- If you enter
1
1
and press the enter key, then the application will add a task to the todo list.
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):
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):
- If you enter
2
2
and press the enter key, then the application will view all tasks in the todo list.
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):
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):
- If you enter
3
3
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.
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):
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):
- If you enter
4
4
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.
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):
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):
- If you enter
E
E
ore
e
and press the enter key, then the application will exit.
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...
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...
- If you enter
R
R
orr
r
and press the enter key, then the application will restart.
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...
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...
- If you enter
H
H
orh
h
and press the enter key, then the application will display the help.
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):
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):
- If you enter any other choice and press the enter key, then the application will display the invalid choice message.
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):
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:
- Add a feature to edit a task.
- Add a feature to sort the tasks.
- Add a feature to search for a task.
- Add a feature to add a task to a specific position.
- Add a feature to add a task to a specific date.
- Add a feature to add a task to a specific time.
- Add a feature to add a task to a specific date and time.
- Add a feature to add a task to a specific date and time and set a reminder.
- Add a feature to add a task to a specific date and time and set a reminder and repeat the task.
- 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 os
os
module, time
time
module, sys
sys
module, and datetime
datetime
module. You also learned how to use the with
with
statement. You also learned how to use the while
while
loop. You also learned how to use the if
if
statement. You also learned how to use the elif
elif
statement. You also learned how to use the else
else
statement. You also learned how to use the def
def
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 del
del
statement. You also learned how to use the sys.exit()
sys.exit()
function. You can find more information about the os
os
module, time
time
module, sys
sys
module, and datetime
datetime
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