MD5 value instances for Python compute files

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.