获取当前目录下所有子目录大小
我主要是用于在C盘查找大文件目录用
测试版 路径填好 直接运行即可
import os
path = r'C:\Users\ys\AppData\LocalLow'
def get_dir_size(dir_path):
# 权限不够 光报错 所以自己跳过了
if dir_path == r'C:\Users\ys\AppData\Local\Packages':
return
size = 0
dir_name = os.path.split(dir_path)[1]
for root, dirs, files in os.walk(dir_path): # os.walk()返回三个结果,因此需要root, dirs, files
size += sum([os.path.getsize(os.path.join(root, name)) for name in files])
return f'{dir_name} ---- {round(size/1024/1024,2)}MB'
dir_name_list = os.listdir(path)
for dir_name in dir_name_list:
dir_path = os.path.join(path,dir_name)
print(get_dir_size(dir_path))
运行效果
4D Door Games ----0.12MB
7th Beat Games ----0.02MB
Adobe ----0.0MB
AzureFlameStudio ----0.09MB
Bushiroad Inc_ ----0.06MB
DefaultCompany ----0.01MB
DNF ----0.15MB
Gameloft ----1.03MB
GSQ ----0.74MB
guigugame ----15.21MB
Kairosoft ----0.02MB
Microsoft ----0.06MB
moyuwan ----0.42MB
MSLiveSticker ----0.0MB
MSLiveStickerWhiteList ----0.0MB
Pathea Games ----43.62MB
SanJiaoMao Studio ----15.27MB
SogouPY ----25.35MB
SogouPY.users ----36.87MB
Studio MDHR ----0.01MB
TENCENT ----0.0MB
Thunder Network ----0.0MB
Unity ----39.86MB
USERJOY ----0.0MB
xfmovie ----0.0MB
xfplay ----0.28MB
XSEED Games _ Marvelous USA, Inc_ ----0.0MB
yusuiInc ----8.91MB
Process finished with exit code 0