Source

woocode / py / sa / statistics / snapshot-file.py

Full commit
#!/usr/bin/env python
# -*- encoding:utf-8 -*-

import os
import sys
from hashlib import md5

def generate_md5(fp, chunk=1024):
    m = md5()
    with open(fp) as fb:
        while True:
            c = fb.read(chunk)
            if not c:
                break
            m.update(c)
    return m.hexdigest()

def get_file_by_path(dirname):
    for root, dirs, files in os.walk(dirname):
        for f in files:
            yield os.path.join(root, f)

def main():
    dirname = sys.argv[1]
    print 'Filename\t\tmd5sum'
    for fp in get_file_by_path(dirname):
        m = generate_md5(fp)
        print "%s: %s" % (fp.ljust(22), m)

if __name__ == '__main__':
    main()