首页 > 解决方案 > 从文件 md5 和文件名中生成一致的 uuid,如哈希

问题描述

我有来自发布 http 请求的文件内容和名称,我想8fa14cdd754f91cc6554c9e71929cce7从文件名和文件 md5中取名

标签: pythonmd5uuid

解决方案


可以用下一个代码来完成

content_hash_md5 = md5()  # noqa: S303
content_hash_md5.update(file_bytes)
content_hash_md5.update(file_name.encode())
base, ext = os.path.splitext(file_name)

new_name = content_hash_md5.hexdigest() + ext

with open(new_name, "wb") as fd:
     fd.write(file_bytes)

推荐阅读