Record#
- Today I learned about file read and write operations.
- A special case is when writing arrays or dictionaries to a file, you need to use the str() function to convert this data into a string.
- When reading it again, you need to use the eval() function to convert the string into dynamically executable code.
- At the beginning of today's class, I was a bit confused about the use of eval(). It wasn't until I practiced coding today that I understood the logic behind it and why we need str() and eval().
- I still don't understand the relationship between writing files and RAM. Where does the usage or knowledge of RAM come into play in this class? I'm still confused.
- Today's exercise is to improve the encoding of 45 and add file writing and reading operations. The code will throw an error when run for the first time, but the instructor said he will solve this problem tomorrow. Hahaha!
CODE#
import os
print("🌟Life Organizer🌟")
print()
todolist = []
todoinfo = []
f = open("to.do", "r")
todolist = eval(f.read())
f.close
def todoprint():
for row in todolist:
for name in row:
print(f"{name: ^10}", end=" | ")
print()
select = "again"
while True:
os.system("clear")
if select == "again":
print("Menu: ")
print("1: Add ")
print("2: Remove ")
print("3: View ")
print("4: Edit ")
menu = input("")
if menu.strip().lower()[0] == "a":
task = input("What is the task? > ")
due = input("When is it due by? > ")
priority = input("What is the priority? > ")
todoinfo = [task, due, priority]
todolist.append(todoinfo)
print("Thanks, this task has been added.")
todoprint()
elif menu.strip().lower()[0] == "r":
name = input("Which one do you want remove? > ")
for row in todolist:
if name in row:
todolist.remove(row)
print("Thanks, this task has been removed.")
else:
print(f"{name} is not in the todolist")
elif menu.strip().lower()[0] == "v":
todoprint()
elif menu.strip().lower()[0] == "e":
name = input("Which one do you want remove? > ")
for row in todolist:
if name in row:
todolist.remove(row)
print("Thanks, this task has been removed.")
else:
print(f"{name} is not in the todolist")
task = input("What is the task? > ")
due = input("When is it due by? > ")
priority = input("What is the priority? > ")
todoinfo = [task, due, priority]
todolist.append(todoinfo)
print("Thanks, this task has been added.")
todoprint()
f = open("to.do", "w")
f.write(str(todolist))
f.close()
elif select == "quit":
break
select = input("Do you want to see the menu again or quit? ")