-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateFileFinder.py
More file actions
81 lines (61 loc) · 1.96 KB
/
Copy pathDuplicateFileFinder.py
File metadata and controls
81 lines (61 loc) · 1.96 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
from sys import *
import os;
import hashlib;
def hashfile(path, blocksize = 1024):
fd = open(path, 'rb')
hasher = hashlib.md5()
buff = fd.read(blocksize)
while len(buff) > 0:
hasher.update(buff)
buff = fd.read(blocksize)
fd.close()
return hasher.hexdigest()
def FindDuplicate(path):
flag = os.path.isabs(path)
if flag == False:
path = os.path.abspath(path)
exists = os.path.isdir(path)
dups = {}
if exists:
for dirName, subdirs, fileList in os.walk(path):
for filen in fileList:
path = os.path.join(dirName, filen)
file_hash = hashfile(path)
if file_hash in dups:
dups[file_hash].append(path)
else:
dups[file_hash] = [path]
return dups
else:
print ("Invalid path")
def PrintDuplicate(dict1):
results = list(filter(lambda x: len(x) > 1, dict1.values()))
if len(results) > 0:
print ("Duplicate files found")
print ("The following are identical files.")
for result in results:
for subresult in result:
print ("\t\t%s" % subresult)
print ("-------------------------------------------------------------------------")
else:
print("No dulplicate files found")
def DisplayHelp():
print ("This script List all duplicate files from provided directory path")
print ("python.exe .\DuplicateFileFinder.py <Directory>")
print ("e.g python.exe .\DuplicateFileFinder.py F:\\Users\DevMJ\\")
def main():
if len(argv) != 2:
print ("ERROR! Invalid arguments")
DisplayHelp()
return
if (argv[1] == "-h") or (argv[1] == "-H"):
DisplayHelp()
return
try:
arr = {}
arr = FindDuplicate(argv[1])
PrintDuplicate(arr)
except ValueError:
print("ERROR: Invalid datatype of imput")
if __name__ == "__main__":
main()