Python simple way to calculate file MD5 Value Example

Source: Internet
Author: User
Tags md5 encryption
This article mainly introduced the Python simple computation file MD5 value method, involves the Python file reading, the hash operation and the MD5 encryption and so on related operation skill, needs the friend to refer to the next

This article describes a simple way to calculate the MD5 value of a file in Python. Share to everyone for your reference, as follows:

A code

Import sysimport Hashlibimport os.pathfilename = sys.argv[1]if os.path.isfile (filename):  fp=open (filename, ' RB ')  contents=fp.read ()  fp.close ()  print (HASHLIB.MD5 (contents). Hexdigest ()) Else:  print (' File not ' Exists ')

Two running results

E:\python\python can learn the 18th Chapter cryptography Programming \code>echo Hello World > Text.txt
E:\python\python can learn the 18th Chapter Cryptography Programming \code>type Text.txt
Hello World
E:\python\python can learn the 18th Chapter Cryptography programming \code>python checkmd5offile.py Text.txt
d1b9c5009a6ddd7dacb45eddb78fa23a
E:\python\python can learn the 18th Chapter cryptography programming \code>echo Hello World1 > Text.txt
E:\python\python can learn the 18th Chapter Cryptography programming \code>python checkmd5offile.py Text.txt
Bed8e00c12f6f2ae01f1d368b7072ac1




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.