-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoCLI.py
More file actions
77 lines (75 loc) · 2.37 KB
/
Copy pathToDoCLI.py
File metadata and controls
77 lines (75 loc) · 2.37 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import json, os
path = r"tasks.json"
empty = 0
try:
with open(path) as file:
file.seek(0)
tasks = json.load(file) # converts to a python list
empty = 0 if not tasks else 1
print("Current tasks:")
for i in range(len(tasks)):
print(f"{i+1}. {tasks[i]}")
except FileNotFoundError:
print("File not found. Creating new...")
with open(path,"w") as file:
json.dump([],file)
print("File Created.")
except Exception as e:
print(f"Error occurred: {e}")
exit()
def methods():
print("\n1. Add\n2. Delete\n3. Quit")
answer = int(input("Option: "))
if answer == 1:
add()
elif answer == 2:
deleteTask() if empty else print("No tasks to delete.")
else:
print("Quitting...")
exit()
return answer
def add():
while True:
task = input("Enter task: ")
try:
with open(path,"+r") as file:
data = json.load(file)
data.append(task)
file.seek(0)
json.dump(data,file,indent=4)
print("Task Added.")
file.truncate()
except Exception as e:
print(f"Error occurred: {e}")
more = input("Add More? [Y/N]: ")
if more.lower() == 'n':
break
def deleteTask():
while True:
delTask = int(input("Task ID to delete: "))
try:
with open(path,"r+") as file:
tasks = json.load(file) # change into python list/array
if (len(tasks) < delTask-1 or delTask <= 0):
print(f"No task with id {tasks}.")
continue
tasks.pop(delTask-1)
file.seek(0)
json.dump(tasks,file,indent=4)
print("Tasks Deleted.")
file.truncate()
except Exception as e:
print(f"Error occurred: {e}")
if not tasks:
print("No more tasks to delete.")
break
delMore = input("Delete more tasks? [Y/N]: ")
if delMore.lower() == 'n':
break
else:
os.system('cls')
# tasks with updated ID's
print("Current tasks: ")
for i in range(len(tasks)):
print(f"{i+1}. {tasks[i]}")
methods()