-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduplicate-files.py
executable file
·47 lines (37 loc) · 1.08 KB
/
duplicate-files.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
#!/usr/bin/env python3
import os
import sys
import hashlib
from pprint import pprint as pp
def travers_dir(dir):
hashes_seen = {}
duplicate_files = []
exclude = ['.git']
for root, dirs, files in os.walk(dir):
dirs[:] = [d for d in dirs if d not in exclude]
path = root.split(os.sep)
# print((len(path) - 1) * '---', os.path.basename(root))
for file in files:
# print(len(path) * '---', file)
fname = dir + '/{}'.format(file)
fname = fname.replace('//', '/')
hash_md5 = hashlib.md5()
try:
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
fhash = hash_md5.hexdigest()
# pp(hashes_seen)
if fhash in hashes_seen:
duplicate_files.append(fname)
duplicate_files.append(hashes_seen[fhash])
# tmp_list = hashes_seen[fhash]
# hashes_seen[fhash] = tmp_list.append(fname)
else:
hashes_seen[fhash] = fname
except OSError as e:
print("Some file was inaccessible...")
pp(hashes_seen)
pp(duplicate_files)
if __name__ == '__main__':
travers_dir(sys.argv[1])