当前位置: 首页 > news >正文

Python计算文件md5

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}")

 

http://www.zskr.cn/news/3258.html

相关文章:

  • CF1774D
  • CF23C
  • CF37C
  • 支持类 Unix 语法 ``:Windows 下用 PowerShell 7 优化 npm 和 VS Code
  • 初赛程序阅读做题要点
  • 模拟堆(手写堆 的五大操作)
  • 完整教程:简单介绍一下Clickhouse及其引擎
  • 矩阵分解
  • 容斥原理
  • 简历优化全攻略:如何写出吸引HR的简历?
  • bashrc的一些配置记录
  • MyEMS与开源浪潮:如何重塑全球能源管理的未来格局
  • doms.ul.querySelectorvs document.querySelector:DOM查询的层级关系
  • Pwn2Own Automotive 2025 决赛日:49个零日漏洞与88万美元奖金揭晓
  • MyEMS在行动:揭秘开源能源管理系统如何重塑工业与楼宇的能效未来
  • 题解:P14015 [ICPC 2024 Nanjing R] 生日礼物
  • HyperWorks许可回收机制
  • flutter开发window打包成exe可执行文件的步骤
  • 基于Linux系统的定制软件安装硬件设备选型指南
  • c++之is_trivially_default_constructible
  • 猫树分治
  • AI导航生成寻路点-FindPathToLocationSynchronously
  • 智聘无界:AI 破解全球化招聘合规、成本与人才匹配难题的实践路径
  • Flink 与Flink可视化平台StreamPark教程(CDC功能)
  • GAS_Aura-Setting Up Auto Running
  • 源码调试-带你了解下车牌识别的深度学习模型-LPRNet
  • charles破解-在线生成激活码
  • 内部排序-直接插入排序冒泡排序快速排序对比
  • C++ auto关键字
  • ARM主板:低功耗高性能的嵌入式计算核心