Python计算文件md5
基础版本
1 import hashlib 2 3 def calculate_md5(file_path, chunk_size=8192): 4 """ 5 计算大文件的MD5值 6 7 Args: 8 file_path (str): 文件路径 9 chunk_size (int): 每次读取的字节数,默认8KB 10 11 Returns: 12 str: MD5值(十六进制字符串) 13 """ 14 md5 = hashlib.md5() 15 16 try: 17 with open(file_path, 'rb') as f: 18 # 分块读取文件,避免内存溢出 19 while True: 20 chunk = f.read(chunk_size) 21 if not chunk: 22 break 23 md5.update(chunk) 24 25 return md5.hexdigest() 26 27 except FileNotFoundError: 28 print(f"文件不存在: {file_path}") 29 return None 30 except Exception as e: 31 print(f"计算MD5时出错: {e}") 32 return None 33 34 # 使用示例 35 if __name__ == "__main__": 36 file_path = "E:\\test.file" 37 md5_value = calculate_md5(file_path) 38 if md5_value: 39 print(f"文件 {file_path} 的MD5值: {md5_value}")
进阶版,带进度条
1 import hashlib 2 3 def calculate_md5_with_progress(file_path, chunk_size=8192): 4 """ 5 计算大文件MD5值并显示处理进度 6 7 Args: 8 file_path (str): 文件路径 9 chunk_size (int): 每次读取的字节数 10 11 Returns: 12 tuple: (MD5值, 文件大小) 13 """ 14 md5 = hashlib.md5() 15 file_size = os.path.getsize(file_path) 16 processed_size = 0 17 18 try: 19 with open(file_path, 'rb') as f: 20 while True: 21 chunk = f.read(chunk_size) 22 if not chunk: 23 break 24 25 md5.update(chunk) 26 processed_size += len(chunk) 27 28 # 显示进度(可选) 29 progress = (processed_size / file_size) * 100 30 if progress % 10 < 0.1: # 每10%显示一次 31 print(f"\r进度: {progress:.1f}%", end='', flush=True) 32 33 print(f"\n计算完成!") 34 return md5.hexdigest(), file_size 35 36 except Exception as e: 37 print(f"计算MD5时出错: {e}") 38 return None, 0 39 40 # 使用示例 41 md5_value, size = calculate_md5_with_progress("E:\\test.file") 42 if md5_value: 43 print(f"MD5值: {md5_value}") 44 print(f"文件大小: {size} bytes")
简洁版本
1 import hashlib 2 3 def quick_md5(file_path): 4 """快速计算文件MD5""" 5 md5 = hashlib.md5() 6 with open(file_path, 'rb') as f: 7 for chunk in iter(lambda: f.read(4096), b""): 8 md5.update(chunk) 9 return md5.hexdigest() 10 11 # 使用示例 12 try: 13 result = quick_md5("E:\\test.file") 14 print(f"MD5: {result}") 15 except Exception as e: 16 print(f"错误: {e}")
本文来自博客园,作者:Arthurian,转载请注明原文链接:https://www.cnblogs.com/Arthurian/p/19088036
欢迎邮件交流:zhuanxinxin@aliyun.com