Method of generating file MD5 checksum function using Python

Source: Internet
Author: User
Objective

In Linux there is a command called md5sum, can generate the MD5 value of the file, in general, the results will be recorded in a file for validation use, such as the use of:

[Crazyant@localhost pythonmd5]$ more Sample_file www.crazyant.netwww.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

Where md5sum-c is used to detect if the generated MD5 value is correct.

Use Python to generate file MD5 values and produce the same result files as md5sum results

Python can use the Hashlib MD5 module to the file content MD5 checksum generation, if you want to generate and md5sum the same result file, only need to MD5 the result value and file name output one line, the middle of a two space output.

Test code:

#-*-Encoding:utf-8-*-from hashlib import md5import os  def generate_file_md5value (fpath): "' takes a file path as a parameter, Returns the value after the file md5 ' ' m = MD5 () # requires a binary format to read the file contents 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, FPA th:%s "% fpath_md5 Fout.flush () fout.close ()  if __name__ = =" __main__ ": Fpath ="/home/users/workbench/pythonmd5/ Sample_file "# Test one: Take the file path as a parameter, get MD5 after the string print Generate_file_md5value (fpath)   # Test Two: Build and Linux command: md5sum the same result. md5 file Generate_file_md5sumfile (Fpath)

Operation Result:

[Crazyant@localhost pythonmd5]$ python generatemd5file.py311d384505e3622ccf85d88930e2b0a0generate success, fpath:/ Home/crazyant/workbench/pythonmd5/sample_file.md5[crazyant@localhost pythonmd5]$ md5sum-c Sample_file.md5sample_ File:ok

Watch out.

Code developed under Windows, if submitted directly to Linux run, often because of Windows line break is \ r \ NAND Linux is \ nthe cause of code execution failure, in general, a conversion.

Summarize

The above is the entire content of this article, I hope that the content of this article on everyone's study or work can bring certain help, if there is doubt you can message exchange. Thank you for your support of topic.alibabacloud.com.

More ways to generate files using Python MD5 check-value function related articles please follow topic.alibabacloud.com!

  • Related Article

    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.