-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.py
More file actions
30 lines (24 loc) · 680 Bytes
/
todo.py
File metadata and controls
30 lines (24 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python3
import json
import os
TODO_FILE = "todos.json"
def load_todos():
if os.path.exists(TODO_FILE):
with open(TODO_FILE) as f:
return json.load(f)
return []
def save_todos(todos):
with open(TODO_FILE, "w") as f:
json.dump(todos, f, indent=2)
def add_todo(task):
todos = load_todos()
todos.append({"task": task, "done": False})
save_todos(todos)
print(f"Added: {task}")
def list_todos():
todos = load_todos()
for i, todo in enumerate(todos, 1):
status = "done" if todo["done"] else "pending"
print(f"{i}. [{status}] {todo['task']}")
if __name__ == "__main__":
list_todos()