hashlib.md5 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3


Hello, what is hashlib.md5 do ?? actually, I wonder if you know what is this function do ?? def md5(fname): if not os.path.isfile(fname): return 'NO FILE' hash_md5 = hashlib.md5() with open(fname, "rb") as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) return hash_md5.hexdigest()

10th Oct 2017, 12:13 PM
hossein hayati
hossein hayati - avatar
1 Answer
+ 1
Short answer: it returns the md5 hash of a file. Long answer: it checks that the variable fname is a file, and if it is it reads the file, in binary mode, in 4096 byte chunks and adds each chunk to the variable hash_md5. When the file is read entirely, the function returns the md5 hash of the file's contents. You can read more about md5 at https://en.m.wikipedia.org/wiki/MD5
14th Oct 2017, 3:58 PM