mariowritescode
mariowritescode

Reputation: 764

MD5 Hash, Python 3 . How to Generate In Python

I need advice on how to get the md5 hash for a zip file. I will be constantly downloading files from an ftp using ftplib. As you know ftplib cannot tell if a file has been modified or not.

I want to use the md5 hash of each new file to tell if it has been modified or not by simply comparing the hashes after downloading the new file to tempdir. If the hashes are similar, I remove newly downloaded file. However, if hashes are different, newly downloaded file is kept, old hash is replaced with new hash and the script continues.

Please advice on how to achieve this. Are there any standalone modules for hashing md5 or similar.

Thanks.``

Upvotes: 4

Views: 3861

Answers (2)

that_roy
that_roy

Reputation: 104

Very simply, in python 3.8+, I use to keep the code as quick and compact as possible.

import hashlib
file_hash = hashlib.md5(open(old_file_path,'rb').read()).hexdigest()
print(file_hash)

Upvotes: 0

user4963716
user4963716

Reputation:

hope this is helpful

import hashlib
m=hashlib.md5();
m.update(open('yourzipfile.zip').read());

a=m.hexdigest()
print (a);

output sh-4.3$ python3 1.py
f5c6a076bd116efbd4b1ce03c96eaf7a

Upvotes: 6

Related Questions