# -*- coding: utf-8 -*-
import hashlib
import os
BUFSIZE = 8192
def getFileMD5(strFilePath):
f = open(strFilePath, 'rb')
m = hashlib.md5()
try:
while True:
data = f.read(BUFSIZE)
if not data:
break
m.update(data)
finally:
f.close()
return m.hexdigest()
def getMD5Info(strPath):
dictMD5 = {}
if os.path.isfile(strPath):
dictMD5[strPath] = getFileMD5(strPath)
else:
if os.path.isdir(strPath):
for strDirPath, lstDirName, lstFileName in os.walk(strPath):
for strFileName in lstFileName:
strTempPath = os.path.join(strDirPath, strFileName)
dictMD5[strTempPath] = getFileMD5(strTempPath)
return dictMD5
if __name__=="__main__":
strFilePath = "C:\\Python27\\ss.py"
print getFileMD5(strFilePath)
strDirPath = "C:\\pkg"
print getMD5Info(strDirPath)
阅读(658) | 评论(0) | 转发(0) |