Creating unique hash for directory in python

Viewed 2741

I would like to create a unique hash in python for a given directory. Thanks to zmo for the code below to generate a hash for every file in a directory but how can I aggregate these to generate a single hash to represent the folder?

import os
import hashlib

def sha1OfFile(filepath):
    sha = hashlib.sha1()
    with open(filepath, 'rb') as f:
        while True:
            block = f.read(2**10) # Magic number: one-megabyte blocks.
            if not block: break
            sha.update(block)
        return sha.hexdigest()

for (path, dirs, files) in os.walk('.'):
  for file in files:
    print('{}: {}'.format(os.path.join(path, file),       
sha1OfFile(os.path.join(path, file)))
2 Answers
Related