-
Notifications
You must be signed in to change notification settings - Fork 0
/
finddup.py
61 lines (42 loc) · 1.62 KB
/
finddup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import hashlib
import itertools
import os
def get_hash(path):
"""Returns the MD5 hash of a file"""
with open(path, 'rb') as file:
file_content = file.read()
hasher = hashlib.md5()
hasher.update(file_content)
return hasher.hexdigest()
def powerwalk(top):
"""Returns an iterator of os.DirEntry objects under top"""
with os.scandir(top) as it:
for entry in it:
yield entry
if entry.is_dir():
yield from powerwalk(entry.path)
def get_duplicate_files(path):
"""Returns a list of duplicate files of a directory in the form of a hash-keyed dictionary"""
file_list = []
for entry in powerwalk(path):
if entry.is_dir():
continue
file_size = entry.stat().st_size
if file_size == 0:
continue
file_list.append((entry.path, file_size))
key = lambda x: x[1]
file_list = sorted(file_list, key=key)
for file_size, file_paths in itertools.groupby(file_list, key=key):
file_paths = [x[0] for x in file_paths]
if len(file_paths) > 1:
file_list = []
for file_path in file_paths:
hash = get_hash(file_path)
file_list.append((file_path, hash))
key = lambda x: x[1]
file_list = sorted(file_list, key=key)
for file_hash, file_paths in itertools.groupby(file_list, key=key):
file_paths = [x[0] for x in file_paths]
if len(file_paths) > 1:
yield file_hash, file_size, file_paths