首页 后端开发 Python教程 Python简单计算文件MD5值的方法示例

Python简单计算文件MD5值的方法示例

Apr 14, 2018 pm 04:05 PM
python 文件 方法

这篇文章主要介绍了Python简单计算文件MD5值的方法,涉及Python文件读取、hash运算及md5加密等相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python简单计算文件MD5值的方法。分享给大家供大家参考,具体如下:

一 代码

import sys
import hashlib
import os.path
filename = sys.argv[1]
if os.path.isfile(filename):
  fp=open(filename,'rb')
  contents=fp.read()
  fp.close()
  print(hashlib.md5(contents).hexdigest())
else:
  print('file not exists')

二 运行结果

E:\python\python可以这样学\第18章 密码学编程\code>echo hello world > text.txt
E:\python\python可以这样学\第18章 密码学编程\code>type text.txt
hello world
E:\python\python可以这样学\第18章 密码学编程\code>python CheckMD5OfFile.py text.txt
d1b9c5009a6ddd7dacb45eddb78fa23a
E:\python\python可以这样学\第18章 密码学编程\code>echo hello world1 > text.txt
E:\python\python可以这样学\第18章 密码学编程\code>python CheckMD5OfFile.py text.txt
bed8e00c12f6f2ae01f1d368b7072ac1




以上是Python简单计算文件MD5值的方法示例的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Python中的类方法是什么 Python中的类方法是什么 Aug 21, 2025 am 04:12 AM

ClassmethodsinPythonareboundtotheclassandnottoinstances,allowingthemtobecalledwithoutcreatinganobject.1.Theyaredefinedusingthe@classmethoddecoratorandtakeclsasthefirstparameter,referringtotheclassitself.2.Theycanaccessclassvariablesandarecommonlyused

如何在Sublime文本中创建一个Python项目? 如何在Sublime文本中创建一个Python项目? Aug 16, 2025 am 08:53 AM

InstallSublimeTextandPython,thenconfigureabuildsystembycreatingaPython3.sublime-buildfilewiththeappropriatecmdandselectorsettingstoenablerunningPythonscriptsviaCtrl B.2.OrganizeyourprojectbycreatingadedicatedfolderwithPythonfilesandsupportingdocument

python asyncio队列示例 python asyncio队列示例 Aug 21, 2025 am 02:13 AM

asyncio.Queue是用于异步任务间安全通信的队列工具,1.生产者通过awaitqueue.put(item)添加数据,消费者用awaitqueue.get()获取数据;2.每处理完一项需调用queue.task_done(),以便queue.join()等待所有任务完成;3.使用None作为结束信号通知消费者停止;4.多个消费者时,需发送多个结束信号或在取消任务前确保所有任务已处理完毕;5.队列支持设置maxsize限制容量,put和get操作自动挂起不阻塞事件循环,程序最终通过canc

如何运行Python脚本并在崇高文本中查看单独面板中的输出? 如何运行Python脚本并在崇高文本中查看单独面板中的输出? Aug 17, 2025 am 06:06 AM

toseepythonOutputiNaseparatePanelInSubliMeText,Usethebuilt-InbuildSystembysavingYourfileWitha.pyExtensionandensionAndPressingCtrl b(orcmd b)

如何避免使用Python的网络刮擦时被阻止? 如何避免使用Python的网络刮擦时被阻止? Aug 16, 2025 am 09:54 AM

ToavoidgettingblockedwhilewebscrapingwithPython,userealisticrequestheaders,addrandomizeddelays,rotateIPaddresseswithproxies,maintainsessions,respectrobots.txt,anduseheadlessbrowserswhennecessary,ensuringethicalandstealthybehaviortomimicrealusersandpr

如何使用Python中的RE模块使用正则表达式? 如何使用Python中的RE模块使用正则表达式? Aug 22, 2025 am 07:07 AM

正则表达式在Python中通过re模块实现,用于搜索、匹配和操作字符串。1.使用re.search()在整个字符串中查找第一个匹配项,re.match()仅在字符串开头匹配;2.用括号()捕获匹配的子组,可命名以提高可读性;3.re.findall()返回所有非重叠匹配的列表,re.finditer()返回匹配对象的迭代器;4.re.sub()替换匹配的文本,支持函数动态替换;5.常用模式包括\d、\w、\s等,可使用re.IGNORECASE、re.MULTILINE、re.DOTALL、re

如何在崇高的文本中构建和运行Python? 如何在崇高的文本中构建和运行Python? Aug 22, 2025 pm 03:37 PM

确保pytythonisinstalledbyrunningpypython-versionorpython3-- versionIntheterminal; ifnotinStalled,下载frompython.organdaddtopath.2.insublimetext,gototools> buildSystem> buildsystem> buildsystem> newbuildsystem

如何在Python中使用变量和数据类型 如何在Python中使用变量和数据类型 Aug 20, 2025 am 02:07 AM

VariablesinPythonarecreatedbyassigningavalueusingthe=operator,anddatatypessuchasint,float,str,bool,andNoneTypedefinethekindofdatabeingstored,withPythonbeingdynamicallytypedsotypecheckingoccursatruntimeusingtype(),andwhilevariablescanbereassignedtodif

See all articles