Skip to content

Instantly share code, notes, and snippets.

@liamHowatt
Created May 24, 2026 13:09
Show Gist options
  • Select an option

  • Save liamHowatt/02b9482818117dce3b85afc8375be750 to your computer and use it in GitHub Desktop.

Select an option

Save liamHowatt/02b9482818117dce3b85afc8375be750 to your computer and use it in GitHub Desktop.
A fairly efficient *dupes implementation
import collections
import os
import hashlib
import filecmp
def main():
sizes = collections.defaultdict(list)
for dirent in os.scandir():
if not dirent.is_file(follow_symlinks=False):
continue
file_size = dirent.stat(follow_symlinks=False).st_size
sizes[file_size].append(dirent.path)
for same_sizes in sizes.values():
if len(same_sizes) < 2:
continue
hashes = collections.defaultdict(list)
for path in same_sizes:
with open(path, "rb") as f:
digest = hashlib.file_digest(f, hashlib.md5)
file_hash = digest.digest()
hashes[file_hash].append(path)
for matches in hashes.values():
while matches:
printed_path0 = False
path0 = matches.pop()
for i in reversed(range(len(matches))):
path1 = matches[i]
if filecmp.cmp(path0, path1, shallow=False):
del matches[i]
if not printed_path0:
printed_path0 = True
print(path0, end='')
print(",", path1, end='')
if printed_path0:
print()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment