Home > Backend Development > Python Tutorial > Python calculation example of md5 value of file

Python calculation example of md5 value of file

高洛峰
Release: 2017-01-16 15:00:00
Original
1343 people have browsed it

Smaller file processing method:

import hashlib
import 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)
Copy after login

Larger file processing method:

import hashlib
import 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)
Copy after login

Description: For the same file, two The md5 calculated by the method is consistent.

Note: The above code passed the test in Python 3.x version.

The above example of calculating the md5 value of a file in python is all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.

For more articles related to python calculation of md5 value examples of files, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template