Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Personal Task Manager in Python using a command-line interface and file I/O for data persistenceCreate O for data persistence #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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()