How to store batch file MD5 codes using Python?
To achieve batch storage of file MD5 codes, follow these steps:
- a library for cryptographic hashing algorithms
import hashlib
- Create a function that calculates the MD5 hash of a file. The function should take the file path as input and return the MD5 hash of the file.
def calculate_md5(file_path):
# 创建一个MD5对象
md5 = hashlib.md5()
# 打开文件并逐行读取内容进行更新
with open(file_path, 'rb') as file:
for line in file:
md5.update(line)
# 返回文件的MD5码
return md5.hexdigest()
- Create a function for calculating the MD5 hash of files in bulk and storing them. The function takes a folder path as input, goes through all the files in the folder, calculates the MD5 hash of each file, and stores the MD5 hash along with the file name in a dictionary.
def batch_calculate_md5(folder_path):
# 创建一个空字典用于存储文件名与MD5码的对应关系
md5_dict = {}
# 遍历文件夹中的所有文件
for file_name in os.listdir(folder_path):
# 拼接文件路径
file_path = os.path.join(folder_path, file_name)
# 计算文件的MD5码
md5 = calculate_md5(file_path)
# 将文件名与MD5码存储到字典中
md5_dict[file_name] = md5
# 返回存储了文件名与MD5码对应关系的字典
return md5_dict
- process multiple files and compute their MD5 checksums on all at once
- a data format commonly used in web development for storing and exchanging data
import json
md5_dict = batch_calculate_md5('/path/to/folder')
# 将字典转换为JSON字符串
json_data = json.dumps(md5_dict)
# 将JSON字符串写入到文件中
with open('/path/to/output_file.json', 'w') as file:
file.write(json_data)
This way, the MD5 checksum of all files in the folder will be calculated and saved to a specified file.