Sometimes, we want to get MD5 hash of big files in Python.
In this article, we’ll look at how to get MD5 hash of big files in Python.
How to get MD5 hash of big files in Python?
To get MD5 hash of big files in Python, we can use the hashlib module.
For instance, we write
import hashlib
with open("your_filename.txt", "rb") as f:
file_hash = hashlib.md5()
while chunk := f.read(8192):
file_hash.update(chunk)
print(file_hash.digest())
print(file_hash.hexdigest())
to read the your_filename.txt file with open as a binary file.
Then we call hashlib.md5 to create the file_hash object.
Next, we call f.read to read the file 8192 bytes as a time.
In the loop, we call file_hash.update to update the hash with the file chunk.
Then we can get the digest and hex digest of the file with digest and hexdigest, which are bytes and strings respectively.
Conclusion
To get MD5 hash of big files in Python, we can use the hashlib module.