Advertisement
zsh2517

Untitled

May 23rd, 2025
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # tpt = Temp PaTh,临时目录
  3.  
  4. import os
  5. import random
  6. from datetime import datetime
  7. import tempfile
  8. import shutil
  9.  
  10.  
  11. def main():
  12.     current_time = datetime.now().strftime("%m%d.%H%M%S")
  13.     rand_num = f"{random.randint(0, 999999):06d}"
  14.     dir_name = f"{current_time}.{rand_num}"
  15.     # TMP = os.environ.get("TMP", tempfile.gettempdir())
  16.     TMP = '/tmp/tpt'
  17.  
  18.     dir_path = os.path.join(TMP, dir_name)
  19.     os.makedirs(dir_path, exist_ok=True)
  20.     os.chdir(dir_path)
  21.     print(f"Entered directory: {os.getcwd()}")
  22.     os.system(os.environ.get("SHELL", "bash") + " --login")
  23.  
  24.  
  25.     # Ctrl+D in SHELL
  26.     os.chdir(TMP)
  27.  
  28.     c = ""
  29.     try:
  30.         c = input(f"Delete directory {dir_path}? (y(d)/n/r) ")
  31.     except EOFError:
  32.         c = "y"
  33.         print()
  34.     cmd = c.split()
  35.     if len(cmd) == 0 or cmd[0] == "y" or cmd[0] == "d":
  36.         # os.rmdir(dir_path)
  37.         shutil.rmtree(dir_path, ignore_errors=True)
  38.         print(f"Deleted directory {dir_path}")
  39.     elif cmd[0] == "n":
  40.         print(f"Directory {dir_path} is not deleted")
  41.     elif cmd[0] == "r":
  42.         # rename
  43.         if len(cmd) < 2 or cmd[1].strip() == "":
  44.             name = input("Input new path: ").strip()
  45.         else:
  46.             name = cmd[1].strip()
  47.         new_dir_path = os.path.join(TMP, name)
  48.         # os.rename(dir_path, new_dir_path)
  49.         shutil.move(dir_path, new_dir_path)
  50.         print(f"Renamed directory {dir_path} to {new_dir_path}")
  51.     else:
  52.         print(f"Invalid input {c}")
  53.  
  54.  
  55. if __name__ == "__main__":
  56.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement