Skip to content

Instantly share code, notes, and snippets.

@mlashcorp
Created March 23, 2011 15:20
Show Gist options
  • Save mlashcorp/883268 to your computer and use it in GitHub Desktop.
Save mlashcorp/883268 to your computer and use it in GitHub Desktop.
Creates a md5 hash using every file in a given folder
#Returns the md5 hash for every file in folder 'folder', except if the
#file is named 'md5' (because that means the folder already contains a md5
#hash)
def md5_for_folder(self,folder, block_size=128):
md5 = hashlib.md5()
for root, dirs, files in os.walk(os.path.join(folder)):
for f in files:
if f!="md5":
fp = open(os.path.join(folder,f),'r')
#Add data for file f to md5 in 128 block size
try:
while True:
data = fp.read(block_size)
if not data:
break
md5.update(data)
except:
traceback.print_exc(file=sys.stdout)
fp.close()
return md5.hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment