-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirDupsRemovalExecTime.py
72 lines (57 loc) · 2.13 KB
/
DirDupsRemovalExecTime.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
"""
Design automation script which accept directory name and delete all duplicate files from that
directory. Write names of duplicate files from that directory into log file named as Log.txt.
Log.txt file should be created into current directory. Display execution time required for the
script.
"""
import sys;
import os;
import time
import Checksum;
def DelDups(dic,starttime):
results = list(filter(lambda x : len(x) > 1 ,dic.values()));
if len(results) > 0:
print("Duplicate files found and deleted!!!!");
with open("OutputDirDupsRemovalExecTime.log",'w') as fobj:
fobj.write("Following Duplicate files are deleted successfully!!!!");
fobj.write("\n" + "---" * 30);
for result in results:
fcnt=0;
for subresult in result:
fcnt+=1;
if fcnt >= 2:
fobj.write("\n%s"%subresult);
os.remove(subresult);
endtime=time.time();
fobj.write("\n\n %s time took to execute."%(endtime-starttime));
print("Output is stored in OutputDirDupsRemovalExecTime.log file");
else:
print("No duplicate files found to delete!!");
def findDups(path):
flag=os.path.isabs(path);
if flag == False:
path=os.path.abspath(path);
exists=os.path.exists(path);
if exists:
dupsfiles={};
for dirName, subDir, files in os.walk(path):
for fname in files:
fname=os.path.join(dirName,fname);
file_hash=Checksum.hashfile(fname);
if file_hash in dupsfiles:
dupsfiles[file_hash].append(fname);
else:
dupsfiles[file_hash]=[fname];
return dupsfiles;
else:
print("Invalid path");
def main():
try:
starttime=time.time();
dirName=sys.argv[1];
arr=findDups(dirName);
DelDups(arr,starttime);
except Exception as E:
print("Exception occured: ",E);
if __name__ == "__main__":
main();