二手产品经理

二手产品经理

THIS IS RENO

os的函數 - 55天 - 在線學python100天

記錄#

  1. 今天學習 os 函數的操作
  2. os.os.listdir () 讀取目錄
  3. os.mkdir ("Hello") 創建目錄
  4. os.rename () 重命名
  5. os.popen (f"cp to.do backup/{bkname}") 複製命令,chatgpt 說這個函數過時了,最新的是 subprocess.run (["cp", "to.do", f"backups/{name}"])。
  6. 今天的練習是修改 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? ")


載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。