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