import os
import time
import random
fileExists = False
try:
myAgenda = []
f = open("calendar.txt", "r")
myAgenda = eval(f.read())
f.close()
except:
fileExists = False
def printList():
print()
for item in myAgenda:
print(item)
print()
while True:
DayPlanner = "Day-planner"
print(f"\033[0;34m{DayPlanner:^35}")
print()
menu = input("Do you want to view, add, delete, remove or edit your to do list?: ")
print()
if menu == "view":
type1 = input("View all? (yes/no): ")
if type1 == "yes":
printList()
type2 = input("View priority (high/low/medium): ")
print()
for item in myAgenda:
if item["priority"] == type2:
print(item)
elif menu == "remove":
item = input("What do you want to remove?: ")
print()
check = input("Are you sure you want to remove this? ")
if check.lower() == "yes":
for entry in myAgenda:
if entry["item"] == item:
myAgenda.remove(entry)
os.system("clear")
time.sleep(2)
break
else:
print("Item does not exist in the list.")
elif check.lower() == "no":
pass
elif menu == "add":
item = input("What's next on the Agenda?: ")
Due = input("When is this due?: ")
priority = input("Priority type (high/low/medium): ")
print()
if not any(entry["item"] == item for entry in myAgenda):
myAgenda.append({"item": item, "due_date": Due, "priority": priority})
os.system("clear")
time.sleep(2)
else:
print("Item already exists in the list.")
elif menu == "edit":
item = input("What do you want to edit?: ")
print()
for entry in myAgenda:
if entry["item"] == item:
new = input("What do you want to change it to?: ")
print()
entry["item"] = new
os.system("clear")
time.sleep(2)
break
else:
print("Item does not exist in the list.")
elif menu == "delete":
break
# this auto saves and should be at the end of the code
if fileExists:
try:
os.mkdir("backup")
except:
pass
name = f"Backups{random.randint(1,1000000000000)}.txt"
os.popen(f"cp calendar.txt backups/{name}")
f = open("calendar.txt", "w")
f.write(str(myAgenda))
f.close()
on the first occurrence of
, you mean True
, not False
- An alternative
printList
function:
def printList():
print("", *myAgenda, "", sep="\n")
Notice that \n
can be used instead of empty print()
to display things on multiple lines
Learn more: print
| iterable unpacking | escape sequences (e.g. \n
)
2. For cross-compat and performance, use shutil.copyfile
to copy files
3.
Ditto: use this instead
print(end="\033c")
2 Likes
aight thanks for the help
Well, I searched for it on the internet, and I found that there are some logical issues with your code.
You can try this code to fix the error.
import os
import time
import random
import shutil # Import the shutil module for file operations
# Check if the "calendar.txt" file exists
fileExists = os.path.isfile("calendar.txt")
try:
myAgenda = []
# Read data from "calendar.txt" if it exists
with open("calendar.txt", "r") as f:
myAgenda = eval(f.read())
except:
fileExists = False
def printList():
print()
for item in myAgenda:
print(item)
print()
while True:
DayPlanner = "Day-planner"
print(f"\033[0;34m{DayPlanner:^35}")
print()
menu = input("Do you want to view, add, delete, remove or edit your to-do list?: ")
print()
if menu == "view":
type1 = input("View all? (yes/no): ")
if type1 == "yes":
printList()
type2 = input("View priority (high/low/medium): ")
print()
for item in myAgenda:
if item["priority"] == type2:
print(item)
elif menu == "remove":
item = input("What do you want to remove?: ")
print()
check = input("Are you sure you want to remove this? ")
if check.lower() == "yes":
for entry in myAgenda:
if entry["item"] == item:
myAgenda.remove(entry)
os.system("clear")
time.sleep(2)
break
else:
print("Item does not exist in the list.")
elif check.lower() == "no":
pass
elif menu == "add":
item = input("What's next on the Agenda?: ")
Due = input("When is this due?: ")
priority = input("Priority type (high/low/medium): ")
print()
if not any(entry["item"] == item for entry in myAgenda):
myAgenda.append({"item": item, "due_date": Due, "priority": priority})
os.system("clear")
time.sleep(2)
else:
print("Item already exists in the list.")
elif menu == "edit":
item = input("What do you want to edit?: ")
print()
for entry in myAgenda:
if entry["item"] == item:
new = input("What do you want to change it to?: ")
print()
entry["item"] = new
os.system("clear")
time.sleep(2)
break
else:
print("Item does not exist in the list.")
elif menu == "delete":
break
# Create a backup if the file exists
if fileExists:
try:
os.mkdir("backups")
except FileExistsError:
pass
name = f"Backups{random.randint(1, 1000000000000)}.txt"
shutil.copy("calendar.txt", os.path.join("backups", name))
# Save the current data to "calendar.txt"
with open("calendar.txt", "w") as f:
f.write(str(myAgenda))
Thanks