#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import hashlib
import time, datetime
from typing import Text
def getMD5(filename, filepath, old="Original MD5"):
with open(filepath, "rb") as fd:
f = fd.read()
pmd5 = hashlib.md5(f)
print(old + filename, pmd5.hexdigest() + "\n")
def changeMd5(filename, filepath):
writefile = int(time.time() * 1000)
with open(filepath, "a") as f:
f.write(str(writefile))
getMD5(filename, filepath, "New MD5")
def findAllFilesWithSpecifiedSuffix(target_dir, target_suffixes=["png", "jpg", "jpeg", "webp"]):
find_res = []
target_suffixes_dot = ["." + suffix for suffix in target_suffixes]
walk_generator = os.walk(target_dir)
for root_path, dirs, files in walk_generator:
if len(files) < 1:
continue
for file in files:
file_name, suffix_name = os.path.splitext(file)
if suffix_name in target_suffixes_dot:
find_res.append([file_name, os.path.join(root_path, file), root_path])
return find_res
imgaRR = findAllFilesWithSpecifiedSuffix(r"C:\Users\Administrator\Desktop\123", ["png", "jpg", "jpeg", "webp"])
for k, v in enumerate(imgaRR):
getMD5(v[0], v[1])
changeMd5(v[0], v[1])
python 批量修改图片MD5值
于 2022-10-25 20:16:38 首次发布