import hashlib def md5_id(file_content_or_path): """计算文件内容或文件路径的MD5哈希值作为ID""" md5_hash = hashlib.md5() # 判断输入是文件内容(bytes)还是文件路径(str) if isinstance(file_content_or_path, bytes): # 直接处理文件内容 md5_hash.update(file_content_or_path) else: # 处理文件路径 with open(file_content_or_path, 'rb') as f: for chunk in iter(lambda: f.read(4096), b''): md5_hash.update(chunk) return md5_hash.hexdigest()