-
Notifications
You must be signed in to change notification settings - Fork 0
/
md5_recursive.py
34 lines (29 loc) · 926 Bytes
/
md5_recursive.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
#!/usr/bin/python2
# coding: utf-8
import hashlib
import sys
import os
def hashfile(afile, hasher, blocksize=65536):
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
return hasher.hexdigest()
def csv_logger(fname, md5hash, outfile):
f = open(outfile, "a")
f.write(fname+","+md5hash+"\n")
f.close()
def main(args):
if len(args) !=3:
sys.exit("use: %s /path/to/recurse outfile.csv" %(args[0]))
for root, dirs, files in os.walk(args[1]):
for file in files:
try:
fname = os.path.join(root,file)
md5hash = hashfile(open(fname, 'rb'), hashlib.md5())
print "%s,%s" %(fname,md5hash)
csv_logger(fname=fname, md5hash=md5hash, outfile=args[2])
except Exception:
pass
if __name__ == "__main__":
main(args=sys.argv)