Generate MD5 Hash of File using Python

hashlib library

import hashlib


def md5_file(filename):
    hash_object = hashlib.md5()

    with open(filename, 'rb') as f:
        for chunk in iter(lambda: f.read(4096), b''):
            hash_object.update(chunk)

    return hash_object.hexdigest()


filename = 'test.txt'
digest = md5_file(filename)

print(digest)

Leave a Comment

Cancel reply

Your email address will not be published.