[Tutor] MD5 Digest for files

Christopher Arndt chris.arndt at web.de
Fri Jan 19 17:49:19 CET 2007


Steve Nelson schrieb:
> I want to create a dictionary of files and md5sums for a given
> directory.  It seems, however, that md5 works with strings or
> read-only buffers, and can't be passed a file.
> 
> What I want to do is something like:
> 
> for f is os.listdir("."):
>   d[f] = someFunctionThatReturnsMD5Sum(f)
> 
> Has this wheel already been invented?  I can't see how to operate on
> the file itself.

Just open and read in the file and then calculate the MD5 sum from the contents:

import os
import md5

def md5sums_for_dir(directory):
    d = {}
    for fn in os.listdir(directory):
        fp = os.path.join(directory, fn)
        if os.path.isfile(fp):
            try:
                fo = open(fp, 'rb')
            except (IOError, OSError):
                print "Could not open file '%s', skipping..." % fp
                continue
            else:
                fcontent = fo.read()
                digest = md5.new()
                digest.update(fcontent)
                d[fn] = digest.hexdigest() # or .digest()
                # the above four lines can be shortened to:
                d[fn] = md5.new(fo.read()).hexdigest()
                fo.close()
    return d


if __name__ == '__main__':
    import sys
    from pprint import pprint
    pprint(md5sums_for_dir(sys.argv[1]))

Chris


More information about the Tutor mailing list