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
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)
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
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!