-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
get_all_md5.py
69 lines (38 loc) · 1.46 KB
/
get_all_md5.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
#!/usr/bin/python
#Calculate MD5 hash for all files in given Directory and Write them to CSV
#Written by: Ajit kumar, [email protected] ,25Feb2015
#No license required for any kind of reuse
#If using this script for your work, please refer this on your willingness
# The CSV file header will have fields as ["fileName","MD5hash"]
#input: path for Directory in which files are stored to calculate MD5
#output: csv file filename and MD5
#import required python modules
import sys
import hashlib
import os
import csv
#MD5 calculation function
def md5sum(filename):
md5 = hashlib.md5()
with open(filename, 'rb') as f:
for chunk in iter(lambda: f.read(128 * md5.block_size), b''):
md5.update(chunk)
return md5.hexdigest()
#change roodir and output values according to your setup
rootdir= "/home/user/ClaMP/test-data"
output= "/home/user/ClaMP/MD5_hash.csv"
count=1
csv_file = open(output,"wa")
writer = csv.writer(csv_file, delimiter=',')
writer.writerow(["fileName","MD5hash"])
for subdir,dirs,files in os.walk(rootdir):
for file in files:
try:
fileHash= md5sum(rootdir + '/' + file)
except Exception, e:
print ("Error while calculating MD5 for the file",file)
else:
print "Writing file number", count
writer.writerow([file,fileHash])
count += 1
csv_file.close()