批量MD5命名文件

时间:2021-04-14 12:26:52
#coding=utf-8
import os
import hashlib

def  GetFileMd5(filename):
    if not os.path.isfile(filename):
        print(filename)
        return
    mymd5 = hashlib.md5()
    f = file(filename,'rb')
    while True:
        b = f.read(8096)
        if not b:
            break
        mymd5.update(b)
    f.close()
    return mymd5.hexdigest().upper()

def bianliDir(foldname):
    for parent,dirnames,filenames in os.walk(foldname):
        for filename in filenames:
            md = GetFileMd5(os.path.join(parent, filename))
            os.rename(os.path.join(parent, filename), os.path.join(parent, md))
            #print(os.path.join(parent, filename))

bianliDir('D:/Download/FileAnalysis')