心 空

  • Tools
Ricky Hao
  1. 首页
  2. Python
  3. 正文

最简单的Python文件校验程序

31 8 月, 2015 1810点热度 0人点赞 0条评论

Python里面自带hashlib库,有几种加密解密的函数,包括md5。
调用md5函数的大致步骤如下:

import hashlib
m=hashlib.md5();
m.update("String");
m.hexdigest();

知道了这个就很好写这个小程序了。
首先,要求用户输入一个文件路径和一个正确的用来校验的md5值。
这里,因为文件基本上会包含.等字符,所以我们要用raw_input()函数来获取路径。
然后,读取文件内容,进行加密。
最后,对比文件内容加密出来的md5和校验值,就可以知道文件是否损坏。

import  hashlib

file_path=raw_input("Please input the path:");
example_md5=raw_input("Please input the md5:");
file_handle=open(file_path,'r');
file_contant=file_handle.read();
m=hashlib.md5();
m.update(file_contant);
if(m.hexdigest() == example_md5):
    print("This is the correct file.");
else:
    print("The file has broken.");
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: python
最后更新:31 8 月, 2015

Ricky

这个人很懒,什么都没留下

点赞
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

COPYRIGHT © 2025 心 空. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang