记录#
- 今天学习 os 函数的操作
- os.os.listdir () 读取目录
- os.mkdir ("Hello") 创建目录
- os.rename () 重命名
- os.popen (f"cp to.do backup/{bkname}") 复制命令,chatgpt 说这个函数过时了,最新的是 subprocess.run (["cp", "to.do", f"backups/{name}"])。
- 今天的练习是修改 51 天的代码,在自动保存之前增加保存到指定目录随机文件名行为。
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? ")