Method to generate the file md5 checksum function using Python

  • 2020-05-19 05:07:41
  • OfStack

preface

In linux, there is a command called md5sum, which can generate md5 value of a file. Normally, the result will be recorded in a file for verification. For example, it will be used like this:


[crazyant@localhost PythonMd5]$ more sample_file 
www.crazyant.net
www.51projob.com
[crazyant@localhost PythonMd5]$ md5sum sample_file > sample_file.md5file
[crazyant@localhost PythonMd5]$ more sample_file.md5file 
311d384505e3622ccf85d88930e2b0a0 sample_file
[crazyant@localhost PythonMd5]$ md5sum -c sample_file.md5file 
sample_file: OK

Among them md5sum -c Used to verify that the generated md5 value is correct.

Use python to generate the md5 value of the file and the result file like md5sum result 1

python can use the md5 module of hashlib to generate the md5 check code for the file contents. If you want to generate a result file like md5sum1, you only need to output the MD5 result value and file name 1 line, with two Spaces in the middle.

Test code:


# -*- encoding:utf-8 -*-
from hashlib import md5
import os
 
def generate_file_md5value(fpath):
 ''' Takes the file path as an argument and returns the pair of files md5 After the value of the 
 '''
 m = md5()
 #  You need to use 2 The base format reads the contents of the file 
 a_file = open(fpath, 'rb') 
 m.update(a_file.read())
 a_file.close()
 return m.hexdigest()
 
def generate_file_md5sumFile(fpath):
 fname = os.path.basename(fpath)
 fpath_md5 = "%s.md5" % fpath
 fout = open(fpath_md5, "w")
 fout.write("%s %s\n" % (generate_file_md5value(fpath), fname.strip()))
 print "generate success, fpath:%s" % fpath_md5
 fout.flush()
 fout.close()
 
if __name__ == "__main__":
 fpath = "/home/users/workbench/PythonMd5/sample_file"
 #  test 1 : take the file path as a parameter to get md5 After the string 
 print generate_file_md5value(fpath)
 
 #  test 2 : the generation and linux Command: md5sum Same result .md5 file 
 generate_file_md5sumFile(fpath)

Operation results:


[crazyant@localhost PythonMd5]$ python generateMd5file.py
311d384505e3622ccf85d88930e2b0a0
generate success, fpath:/home/crazyant/workbench/PythonMd5/sample_file.md5
[crazyant@localhost PythonMd5]$ md5sum -c sample_file.md5
sample_file: OK

Pay attention to the point

Code developed under windows, if submitted directly to linux for running, often fails to execute because the newline character under windows is \r\n and linux is \n. In general, 1 conversion is required.

conclusion


Related articles: