Code source de ensae_teaching_cs.homeblog.utils_file

"""
Reasonably inefficient functions about files.


:githublink:`%|py|5`
"""
import hashlib


[docs]def checksum_md5(filename): """ computes MD5 for a file :param filename: filename :return: string or None if there was an error :githublink:`%|py|12` """ fname = filename block_size = 0x10000 fd = open(fname, "rb") try: block = [fd.read(block_size)] while len(block[-1]) > 0: block.append(fd.read(block_size)) contents = block zero = hashlib.md5() i = 0 for el in contents: i += 1 zero.update(el) m = zero return m.hexdigest() finally: fd.close() return None