All Projects
beginner
To-Do Application
A command-line to-do list application with add, view, complete, and delete functionality with file persistence.
Features
- Add/remove tasks
- Mark tasks as complete
- File-based persistence
- Priority levels
- List all tasks
Technologies Used
File HandlingListsDictionariesFunctionsJSON
Explanation
Tasks are stored as dictionaries in a list and saved to a JSON file for persistence. Users can add, view, complete, and delete tasks through a menu-driven interface.
import json
import os
from datetime import datetime
TASKS_FILE = "tasks.json"
def load_tasks():
if os.path.exists(TASKS_FILE):
with open(TASKS_FILE, "r") as f:
return json.load(f)
return []
def save_tasks(tasks):
with open(TASKS_FILE, "w") as f:
json.dump(tasks, f, indent=2)
def add_task(tasks):
title = input("Task title: ")
priority = input("Priority (high/medium/low): ").lower()
task = {
"id": len(tasks) + 1,
"title": title,
"priority": priority,
"completed": False,
"created": datetime.now().strftime("%Y-%m-%d %H:%M")
}
tasks.append(task)
save_tasks(tasks)
print(f"✅ Task '{title}' added!")
def view_tasks(tasks):
if not tasks:
print("No tasks found!")
return
print("\n--- Your Tasks ---")
for t in tasks:
status = "✓" if t["completed"] else "○"
print(f" [{status}] {t['id']}. {t['title']} ({t['priority']}) - {t['created']}")
print()
def complete_task(tasks):
view_tasks(tasks)
task_id = int(input("Enter task ID to complete: "))
for t in tasks:
if t["id"] == task_id:
t["completed"] = True
save_tasks(tasks)
print(f"✅ Task '{t['title']}' completed!")
return
print("Task not found!")
def delete_task(tasks):
view_tasks(tasks)
task_id = int(input("Enter task ID to delete: "))
tasks[:] = [t for t in tasks if t["id"] != task_id]
save_tasks(tasks)
print("🗑️ Task deleted!")
def main():
print("=== To-Do Application ===")
tasks = load_tasks()
while True:
print("\n1. Add Task 2. View Tasks 3. Complete 4. Delete 5. Exit")
choice = input("Choose: ")
if choice == "1": add_task(tasks)
elif choice == "2": view_tasks(tasks)
elif choice == "3": complete_task(tasks)
elif choice == "4": delete_task(tasks)
elif choice == "5":
print("Goodbye!")
break
main()