Skip to content

Instantly share code, notes, and snippets.

@F-atal1ty
Forked from juusimaa/CalculateMd5
Created March 8, 2022 02:00
Show Gist options
  • Save F-atal1ty/8949ad0dbd128d277a05da79767738ee to your computer and use it in GitHub Desktop.
Save F-atal1ty/8949ad0dbd128d277a05da79767738ee to your computer and use it in GitHub Desktop.
Python (3.x) function to calculate MD5 checksum for given file.
def calculateMD5(filename, block_size=2**20):
"""Returns MD% checksum for given file.
"""
import hashlib
md5 = hashlib.md5()
try:
file = open(filename, 'rb')
while True:
data = file.read(block_size)
if not data:
break
md5.update(data)
except IOError:
print('File \'' + filename + '\' not found!')
return None
except:
return None
return md5.hexdigest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment