Home > Backend Development > Python Tutorial > How to use Python to generate file md5 check value function

How to use Python to generate file md5 check value function

高洛峰
Release: 2017-01-14 11:48:00
Original
3043 people have browsed it

Foreword

There is a command in Linux called md5sum, which can generate the md5 value of a file. Under normal circumstances, 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
Copy after login

Among them, md5sum -c is used to check whether the generated md5 value is correct.

Use python to generate file md5 values ​​and generate result files that are the same as md5sum results

Python can use hashlib's md5 module to generate md5 check codes for file contents. If you want to generate the same result file as md5sum, you only need to output the MD5 result value and file name in one line, with two spaces in the middle.

Test code:

# -*- encoding:utf-8 -*-
from hashlib import md5
import os
  
def generate_file_md5value(fpath):
 '''以文件路径作为参数,返回对文件md5后的值
 '''
 m = md5()
 # 需要使用二进制格式读取文件内容
 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"
 # 测试一:以文件路径作为参数,获得md5后的字符串
 print generate_file_md5value(fpath)
  
 # 测试二:生成和linux命令:md5sum同样结果的.md5文件
 generate_file_md5sumFile(fpath)
Copy after login

Running result:

[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
Copy after login

be careful

If the code developed under Windows is directly submitted to Linux for running, the code execution often fails because the newline character under Windows is \r\n but Linux is \n. Generally, a conversion is required.

Summarize

The above is the entire content of this article. I hope the content of this article can be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to the PHP Chinese website.

For more related articles on how to use Python to generate file md5 check value function, 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