Home > Article > Backend Development > Example of a simple method to calculate the MD5 value of a file in Python
This article mainly introduces Python's simple method of calculating the MD5 value of a file, involving Python file reading, hash operation and md5 encryption and other related operating skills. Friends in need can refer to the following
This article describes the examples Python simple method to calculate the MD5 value of a file. Share it with everyone for your reference, the details are as follows:
一码
##
import sys import hashlib import os.path filename = 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')
Second running results
E:\python\python can be learned in this way\Chapter 18 Cryptozoology Programming\code>echo hello world> text.txtE:\python\python can be learned in this way\Chapter 18 Cryptographic Programming\code>type text.txt
hello world
E:\python\python can be learned in this way\Chapter 18 Cryptographic Programming \code>python CheckMD5OfFile.py text.txt
d1b9c5009a6ddd7dacb45eddb78fa23a
E:\python\python can be learned like this\Chapter 18 Cryptozoology Programming\code>echo hello world1 > text.txt
E:\ python\python can be learned like this\Chapter 18 Cryptographic Programming\code>python CheckMD5OfFile.py text.txt
bed8e00c12f6f2ae01f1d368b7072ac1
The above is the detailed content of Example of a simple method to calculate the MD5 value of a file in Python. For more information, please follow other related articles on the PHP Chinese website!