Python :check大文件或者文件夾中所有文件MD5值

hashlib.md5獲得打開文件的md5值,但是當文件很大的時候,比如好幾個G,就會吃掉過多的內存,有沒有辦法在不打開文件的情況下,獲得大文件的md5值呢?或者給出特定文件夾,check出文件夾中所有文件的MD5值並且寫入特定文件中???


以下代碼可以做到:





from hashlib import md5
import time
import os


def calMD5(str):     #check string的MD5值
    m = md5()
    m.update(str)
    return m.hexdigest()
    
    
    
def calMD5ForFile(file):         #check文件的MD5值
    statinfo = os.stat(file)
    if int(statinfo.st_size)/(1024*1024) >= 1000 :
        print ("File size > 1000, move to big file...")
        return calMD5ForBigFile(file)
    m = md5()
    f = open(file, 'rb')
    m.update(f.read())
    f.close()
    return m.hexdigest()
    
    
    
def calMD5ForFolder(dir,MD5File):     #check文件夾的MD5值
    outfile = open(MD5File,'w')
    for root, subdirs, files in os.walk(dir):
        for file in files:
            filefullpath = os.path.join(root, file)
            """print filefullpath"""
            filerelpath = os.path.relpath(filefullpath, dir)
            md5 = calMD5ForFile(filefullpath)
            print(md5)
            outfile.write(filerelpath+"\t\t******-----------******\t\t"+md5+"\n")
    outfile.close()
    
    
    
def calMD5ForBigFile(file):    #check大文件的MD5值
    m = md5()
    f = open(file, 'rb')
    buffer = 8192    # why is 8192 | 8192 is fast than 2048
    while 1:
        chunk = f.read(buffer)
        if not chunk : break
        m.update(chunk)
    f.close()
    return m.hexdigest()
    
    
    
checkmd5 = calMD5ForFolder(r'D:\software',r'C:\Users\Desktop\a.txt')
print(checkmd5)



親測,,,很好用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章