-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreader.py
68 lines (58 loc) · 2.19 KB
/
threader.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
62
63
64
65
66
67
68
import os
import shutil
import hashlib
import string
import random
from concurrent.futures import ThreadPoolExecutor, as_completed
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def hash_file(file_path):
try:
with open(file_path, 'rb') as f:
file_hash = hashlib.md5(f.read()).hexdigest()
return file_hash, file_path
except Exception as e:
print(f"{FAIL}[-]{ENDC} Error hashing file {file_path}: {e}")
return None
def remove_duplicates(dir_path):
hash_dict = {}
with ThreadPoolExecutor(max_workers=8) as executor:
futures = []
for root, dirs, files in os.walk(dir_path):
for file in files:
file_path = os.path.join(root, file)
futures.append(executor.submit(hash_file, file_path))
for future in as_completed(futures):
result = future.result()
if result is not None:
file_hash, file_path = result
if file_hash in hash_dict:
try:
os.remove(file_path)
print(f"{FAIL}[-]{ENDC} {file_path} removed due to duplicate hash")
except Exception as e:
print(f"{FAIL}[-]{ENDC} Error removing file {file_path}: {e}")
else:
hash_dict[file_hash] = file_path
print(f"{OKGREEN}[+]{ENDC} {file_path} added to hash dictionary")
output_file = os.path.join(dir_path, "MD5_hashes.txt")
try:
with open(output_file, 'w') as f:
for file_path, file_hash in hash_dict.items():
f.write(f"{file_hash} {file_path}\n")
except Exception as e:
print(f"{FAIL}[-]{ENDC} Error writing to file {output_file}: {e}")
return output_file
if __name__ == '__main__':
directory = input("Enter directory path to sort files: ")
try:
remove_duplicates(directory)
except Exception as e:
print(f"{FAIL}[-]{ENDC} Error removing duplicates: {e}")