Python 计算文件MD5散列值

用hashlib库都会算md5散列值,总得要做点什么不同的才能水一篇文章嘛(笑)。那不如我们写的一个python脚本,用鼠标把需要计算的文件拖曳上去就能得到计算结果,“岂不美哉.jpg”。

文件拖曳

注册表中的配置

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Python.File\shellex\DropHandler]
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"

获取拖曳文件路径

import sys
filename=sys.argv[1:]

demo

import sys
from hashlib import md5

paths = sys.argv[1:]
for path in paths:
    with open(path,"rb") as fd:
        fmd5 = md5(fd.read())
        print(f'md5({path}):', fmd5.hexdigest())
input()

参考

  1. 如何拖动文件到python脚本中作为输入参数? http://blog.csdn.net/eijnew/article/details/6695271/
  2. Drag and drop onto Python script in Windows Explorer http://stackoverflow.com/questions/142844/drag-and-drop-onto-python-script-in-windows-explorer
  3. Make Python Scripts Droppable in Windows http://mindlesstechnology.wordpress.com/2008/03/29/make-python-scripts-droppable-in-windows/

留下评论