check_repeat.py 1.3 KB

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