# -*- coding: utf-8 -*- import os # 字节b转化kb\m\g def format_size(b): try: b = float(b) kb = b / 1024 except: print("传入的字节格式不对") return "Error" if kb >= 1024: M = kb / 1024 if M >= 1024: G = M / 1024 return "%fG" % (G) else: return "%fM" % (M) else: return "%fkb" % (kb) # 获取文件夹大小 def get_file_size(path): filename = os.walk(path) for root, dirs, files in filename: for file in files: try: size = os.path.getsize(os.path.join(root, file)) # 104857600 100M if size > 104857600: print(f'文件 {os.path.join(root, file)} 大小 {format_size(size)}') except Exception as err: print(err) if __name__ == "__main__": get_file_size('D:\\')
标签:1024,找出,kb,Windows,文件夹,file,path,os,size From: https://www.cnblogs.com/daizichuan/p/17488733.html