check_repeat.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import os
  2. import shutil
  3. import argparse
  4. def find_and_delete_duplicate_files(path):
  5. # 字典,用于跟踪文件名及其出现的路径
  6. file_paths = {}
  7. # 遍历目录及其子目录
  8. for root, dirs, files in os.walk(path):
  9. for file in files:
  10. file_path = os.path.join(root, file)
  11. # 如果文件名已经在字典中,则添加其路径
  12. if file in file_paths:
  13. file_paths[file].append(file_path)
  14. else:
  15. file_paths[file] = [file_path]
  16. # 查找并删除重复文件
  17. for file, paths in file_paths.items():
  18. if len(paths) > 1:
  19. # 保留第一个文件,删除其他文件
  20. to_keep = paths[0]
  21. for path in paths[1:]:
  22. try:
  23. print(f"Deleting duplicate file: {path}")
  24. os.remove(path)
  25. except OSError as e:
  26. print(f"Error deleting file {path}: {e}")
  27. def main():
  28. parse = argparse.ArgumentParser("查找重复文件并删除\n")
  29. parse.add_argument("input", help="输入路径")
  30. args = parse.parse_args()
  31. find_and_delete_duplicate_files(args.input)
  32. if __name__ == "__main__":
  33. main()