zsh2517

Untitled

Nov 9th, 2024
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #conding=utf8
  2. import os
  3. import datetime
  4. # g = os.walk(r"e:\test")
  5.  
  6. # for path,dir_list,file_list in g:
  7. # for file_name in file_list:
  8. # print(os.path.join(path, file_name) )1
  9.  
  10. p = r"C:\Users\zsh2517\Documents\tencent files\xxxx\Image\group2"
  11. del_time = datetime.datetime.fromisoformat("2023-06-01 00:00:00").timestamp()
  12. count_f = 0
  13. count_d = 0
  14. def empty(path):
  15. if len(os.listdir(path)) == 0:
  16. return True
  17. else:
  18. return False
  19.  
  20. def show():
  21. global count_f, count_d
  22. if (count_f + count_d) % 100 == 0:
  23. print(f"删除旧文件 {count_f} 个,删除空目录 {count_d} 个,总共 {count_d + count_f} 个")
  24.  
  25. def clear(fa_path):
  26. global count_f, count_d
  27. l = os.listdir(fa_path)
  28. for f in l:
  29. path = fa_path + "\\" + f
  30. if os.path.isfile(path):
  31. if os.stat(path).st_mtime < del_time:
  32. os.remove(path)
  33. # print(f"delete file {path}")
  34. count_f += 1
  35. show()
  36. else:
  37. if empty(path):
  38. os.rmdir(path)
  39. # print(f"delete path {path}")
  40. count_d += 1
  41. show()
  42. else:
  43. clear(path)
  44. if empty(path):
  45. os.rmdir(path)
  46. # print(f"delete path {path}")
  47. count_d += 1
  48. show()
  49.  
  50. clear(p)
  51. print(f"删除文件 {count_f} 个,删除目录 {count_d} 个,总共 {count_d + count_f} 个")
  52. os.system("pause")
Advertisement
Add Comment
Please, Sign In to add comment