Extra Newby question - Trying to create md5 File Listing

utabintarbo utabintarbo at gmail.com
Thu Sep 28 15:06:46 EDT 2006


Just for S&G's, I offer the following function which will return the
md5sum of an arbitrarily large file. Found a while back while
surfing....

def md5sum(fpath):
    """
    function to return the md5sum (128 bit checksum) of the given file

    The sums are computed as described in RFC 1321. The default mode is
binary.
    Output is a line with checksum and name for each FILE.
    """
    import md5
    import os
    bufsize = 8096
    rmode = 'rb'

    m = md5.new()
    fp = open(fpath, rmode)
    try:
        while 1:
            data = fp.read(bufsize)
            if not data:
                break
            m.update(data)
    except IOError, msg:
        return '0', msg
    return m.hexdigest() + ' ' + fpath

Enjoy!




More information about the Python-list mailing list