Skip to content

Commit 90f3fc0

Browse files
authored
Create todo.py
1 parent 46d4f9b commit 90f3fc0

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

todo.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import json
2+
import os
3+
import sys
4+
5+
TASKS_FILE = "tasks.json"
6+
7+
def load_tasks():
8+
if not os.path.exists(TASKS_FILE):
9+
return []
10+
with open(TASKS_FILE, "r") as f:
11+
return json.load(f)
12+
13+
def save_tasks(tasks):
14+
with open(TASKS_FILE, "w") as f:
15+
json.dump(tasks, f, indent=4)
16+
17+
def add_task(description):
18+
tasks = load_tasks()
19+
tasks.append({"task": description})
20+
save_tasks(tasks)
21+
print(f"Added task: {description}")
22+
23+
def list_tasks():
24+
tasks = load_tasks()
25+
if not tasks:
26+
print("No tasks found.")
27+
else:
28+
for i, task in enumerate(tasks, 1):
29+
print(f"{i}. {task['task']}")
30+
31+
def remove_task(index):
32+
tasks = load_tasks()
33+
if 1 <= index <= len(tasks):
34+
removed = tasks.pop(index - 1)
35+
save_tasks(tasks)
36+
print(f"Removed task: {removed['task']}")
37+
else:
38+
print("Invalid task number.")
39+
40+
def show_help():
41+
print("To-Do List Manager")
42+
print("Usage:")
43+
print(" python todo.py add 'Task description'")
44+
print(" python todo.py list")
45+
print(" python todo.py remove [task number]")
46+
47+
if __name__ == "__main__":
48+
if len(sys.argv) < 2:
49+
show_help()
50+
else:
51+
command = sys.argv[1]
52+
if command == "add" and len(sys.argv) >= 3:
53+
add_task(" ".join(sys.argv[2:]))
54+
elif command == "list":
55+
list_tasks()
56+
elif command == "remove" and len(sys.argv) == 3:
57+
try:
58+
remove_task(int(sys.argv[2]))
59+
except ValueError:
60+
print("Please provide a valid task number.")
61+
else:
62+
show_help()

0 commit comments

Comments
 (0)