From 434500c4da3f688d893eada1c1f7ee57d3d69ac4 Mon Sep 17 00:00:00 2001 From: Santhosh Krishnan <165752419+7358897922@users.noreply.github.com> Date: Mon, 8 Apr 2024 14:45:47 +0530 Subject: [PATCH] Create O for data persistence --- .../O for data persistence | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Personal Task Manager in Python using a command-line interface and file I/O for data persistence diff --git a/Personal Task Manager in Python using a command-line interface and file I/O for data persistence b/Personal Task Manager in Python using a command-line interface and file I/O for data persistence new file mode 100644 index 0000000..6d49ae3 --- /dev/null +++ b/Personal Task Manager in Python using a command-line interface and file I/O for data persistence @@ -0,0 +1,94 @@ +import json +import os +from datetime import datetime + +# Function to load tasks from the file +def load_tasks(): + if os.path.exists("tasks.json"): + with open("tasks.json", "r") as file: + return json.load(file) + else: + return [] + +# Function to save tasks to the file +def save_tasks(tasks): + with open("tasks.json", "w") as file: + json.dump(tasks, file, indent=4) + +# Function to display all tasks +def display_tasks(tasks): + print("All Tasks:") + if tasks: + for index, task in enumerate(tasks, start=1): + print(f"{index}. {task['title']} - {task['description']} - Due: {task['due_date']} - Status: {task['status']}") + else: + print("No tasks found.") + +# Function to add a new task +def add_task(tasks): + title = input("Enter task title: ") + description = input("Enter task description: ") + due_date = input("Enter due date (YYYY-MM-DD): ") + status = "Pending" + tasks.append({"title": title, "description": description, "due_date": due_date, "status": status}) + save_tasks(tasks) + print("Task added successfully.") + +# Function to update a task +def update_task(tasks): + display_tasks(tasks) + try: + index = int(input("Enter task number to update: ")) - 1 + if 0 <= index < len(tasks): + tasks[index]["title"] = input("Enter new title (leave blank to keep existing): ") or tasks[index]["title"] + tasks[index]["description"] = input("Enter new description (leave blank to keep existing): ") or tasks[index]["description"] + tasks[index]["due_date"] = input("Enter new due date (YYYY-MM-DD) (leave blank to keep existing): ") or tasks[index]["due_date"] + tasks[index]["status"] = input("Enter new status (leave blank to keep existing): ") or tasks[index]["status"] + save_tasks(tasks) + print("Task updated successfully.") + else: + print("Invalid task number.") + except ValueError: + print("Invalid input. Please enter a number.") + +# Function to delete a task +def delete_task(tasks): + display_tasks(tasks) + try: + index = int(input("Enter task number to delete: ")) - 1 + if 0 <= index < len(tasks): + del tasks[index] + save_tasks(tasks) + print("Task deleted successfully.") + else: + print("Invalid task number.") + except ValueError: + print("Invalid input. Please enter a number.") + +# Main function +def main(): + tasks = load_tasks() + while True: + print("\n===== Personal Task Manager =====") + print("1. View all tasks") + print("2. Add a new task") + print("3. Update a task") + print("4. Delete a task") + print("5. Exit") + choice = input("Enter your choice: ") + if choice == "1": + display_tasks(tasks) + elif choice == "2": + add_task(tasks) + elif choice == "3": + update_task(tasks) + elif choice == "4": + delete_task(tasks) + elif choice == "5": + print("Exiting...") + break + else: + print("Invalid choice. Please try again.") + +if __name__ == "__main__": + main()