forked from arsh939/Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindDuplicateFilesInDir.py
53 lines (46 loc) · 1.71 KB
/
FindDuplicateFilesInDir.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
#!/usr/bin/python
'''
A python script to get duplicate files by comparing hashes of files in all sub-directories of a directory
'''
import hashlib
import os
import json
All_Files_Hash ={}
def Get_MD5_File_Hash(FilePath):
hash_md5 = hashlib.md5()
try:
with open(FilePath,"rb") as file:
for chunk in iter(lambda: file.read(4096), b""):
hash_md5.update(chunk)
except Exception as ex:
print(ex)
return "ss"
return hash_md5.hexdigest()
def Generate_Hashes_Of_Files_And_Search_Duplicates(rootDirPath):
for dir, subDir, files in os.walk(rootDirPath):
for file in files:
filePath = os.path.join(dir,file)
fileHash = Get_MD5_File_Hash(filePath)
if(fileHash=="ss"):
continue
if fileHash in All_Files_Hash:
All_Files_Hash[fileHash].append(filePath)
print(f"Duplicate: {All_Files_Hash[fileHash]}")
else:
temp=[]
temp.append(filePath)
All_Files_Hash[fileHash]=temp
#Needs some work
def Save_Data_To_JSON_File():
with open("DuplicateFiles.json","w+") as f:
json.dump(All_Files_Hash,f,indent=4)
if __name__ == "__main__":
DirectoryPath="sudesh_Path_Doesnt_exist"
while(os.path.exists(DirectoryPath)==False):
DirectoryPath = input("Enter full path of directory to search: ")
if(os.path.exists(DirectoryPath)==False):
print(f"Path '{DirectoryPath}' does not exist!")
print("Search started to find duplicates...")
Generate_Hashes_Of_Files_And_Search_Duplicates(DirectoryPath)
# Save_Data_To_JSON_File()
pause = input("Search Completed. Press any key to continue!!")