Search code examples
pythonmd5uuid

Make consistent uuid like hash from file md5 and file name


I have filecontent and name from post http request and I want to make name like 8fa14cdd754f91cc6554c9e71929cce7 from filename and file md5


Solution

  • It can be done with next code

    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)