Record#
- Today I learned about operating system functions.
- os.os.listdir() is used to read directories.
- os.mkdir("Hello") is used to create directories.
- os.rename() is used for renaming.
- os.popen(f"cp to.do backup/{bkname}") is a copy command. ChatGPT says that this function is outdated and the latest one is subprocess.run(["cp", "to.do", f"backups/{name}"]).
- Today's exercise is to modify the code for day 51 and add the behavior of saving to a specified directory with a random file name before automatic saving.
CODE#
import os, random
print("🌟Life Organizer🌟")
print()
todolist = []
todoinfo = []
try:
f = open("to.do", "r")
todolist = eval(f.read())
f.close
except:
fileExists = False
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()
files = os.listdir()
if "backup" not in files:
os.mkdir("backup")
if "to.do" in files:
bkname = f"{random.randint(1,99999999)}.txt"
os.popen(f"cp to.do backup/{bkname}")
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? ")