-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectoryDisplayDuplicateFile-5.py
92 lines (69 loc) · 2.31 KB
/
DirectoryDisplayDuplicateFile-5.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from sys import *
import os
import hashlib
def hashfile(path,blocksize=1024):
afile = open(path,'rb')
hasher = hashlib.md5()
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
afile.close()
return hasher.hexdigest()
def findDuplicateFileDiplay(path):
flag = os.path.isabs(path)
if flag ==False:
path = os.path.abspath(path)
exists = os.path.isdir(path)
dups = {}
if exists:
for dirName , SubDir, fileNames in os.walk(path):
print("Current directory is : "+dirName)
for files in fileNames:
path = os.path.join(dirName,files)
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")
exit()
def printDuplicates(dict1):
results = list(filter(lambda x : len(x)>1, dict1.values()))
icnt = 0
if len(results)>0:
print("Duplicates found : ")
print("The following files are identetical")
for result in results:
for subres in result:
icnt +=1
if icnt >=2:
print('')
print('\t\t%s' % subres , "\n")
icnt = 0
else:
print("No Duplicates are found ")
def main():
print("Marvellous Infosystems by Sidheshwar Jawale")
print("Application Name : "+argv[0])
if(len(argv)!=2):
print("ERROR : INSUFFICIENT ARGUMENTS")
exit()
if(argv[1]=='-u') or (argv[1] == '-U'):
print("USAGE : ApplicationName AbsolutePath_of_Directory Extension")
exit()
if(argv[1] == "-h" ) or (argv[1] == '-H'):
print("HELP :The script is used to traverse specific directory and display all files which have given Extension ")
exit()
try:
arr = {}
arr = findDuplicateFileDiplay(argv[1])
printDuplicates(arr)
except ValueError as v:
print("Error : Invalid Datatype of input",v)
except Exception as E :
print("Error : invalid input" ,E)
if __name__ == '__main__':
main()