import hashlib import os def GetFileMd5(filename): if not os.path.isfile(filename): return myHash = hashlib.md5() f = open(filename,'rb') while True: b = f.read(8096) if not b : break myHash.update(b) f.close() return myHash.hexdigest() print(GetFile
下面提供一种利用os.walk()快捷的计算指定文件夹大小的方法 话不多说,直接上代码 简略版: import os def get_file_size(file_path, size=0): for root, dirs, files in os.walk(file_path): for f in files: size += os.path.getsize(os.path.join(root, f)) # 加上下面一行打印所有文件 # print(f) return size print(g
def GetFileMd5(filename): if not os.path.isfile(filename): return myhash = hashlib.md5() f = file(filename,'rb') while True: b = f.read(8096) if not b : break myhash.update(b) f.close() return myhash.hexdigest()