Small File handling methods:
Import Hashlibimport os def get_md5_01 (file_path): MD5 = None if Os.path.isfile (file_path): f = open (File_ Path, ' RB ') md5_obj = Hashlib.md5 () md5_obj.update (F.read ()) Hash_code = Md5_obj.hexdigest () F.close () MD5 = str (hash_code). Lower () return MD5 if __name__ = "__main__": File_path = R ' D:\test\test.jar ' md5_01 = get_md5_01 (file_path) print (md5_01)
Large File Handling methods:
Import Hashlibimport os def get_md5_02 (file_path): f = open (File_path, ' RB ') md5_obj = HASHLIB.MD5 () While True: d = f.read (8096) if not D: break md5_obj.update (d) Hash_code = Md5_obj.hexdigest () f.close () MD5 = str (hash_code). Lower () return MD5 if __name__ = = "__main__": File_path = R ' D:\ Test\test.jar ' md5_02 = get_md5_02 (file_path) print (md5_02)
Note: For the same file, the two methods calculated by the MD5 are consistent.
Note: The above code is tested in Python version 3.x.
Above this python computation file MD5 value instance is small to share to everybody's all content, hoped can give everybody a reference, also hoped that everybody many support topic.alibabacloud.com.