统计一个目录下的子目录的Size(清理Program Files文件夹神器)
- 作者: 我可是巴拉巴拉小魔仙呢
- 来源: 51数据库
- 2022-08-12
#----------------------------------------------------------------
# -*- coding: utf-8 -*-
#!/usr/bin/env python
#----------------------------------------------------------------
# Author : pcfeng502
#
# E-Mail : pcfeng502@126.com
#
# File : folderSizeList_v02.py
#
# Introduction:
# 统计一个文件夹下的子文件夹的大小
# 方便删除文件夹中的子文件夹
#----------------------------------------------------------------
# works with Python 3.3.2; windows 7 64bit
import os
from os.path import join
from os.path import getsize
exceptionCount = 0;
def getDirSize(dir):
size = 0;
if os.path.isdir(dir):
for root, dirs, files in os.walk(dir):
try:
size += sum(getsize(join(root, name)) for name in files)
except FileNotFoundError:
global exceptionCount
exceptionCount +=1
return size;
else:
size = getsize(dir)
return size
def getSubDir(dir):
subDirList = os.listdir(dir)
return subDirList
#todo
##def getpath():
if __name__ == '__main__':
#TODO read the file name input
path = input('Input the path you want to check out size\n');
rootpath = path;
print(rootpath);
subdir = getSubDir(rootpath);
wholeDirSize = 0;
subDirSize = [];
print('There are', len(subdir), 'files+folders in', rootpath);
for i in range(len(subdir)):
subDirSize.append(getDirSize(join(rootpath, subdir[i])));
wholeDirSize += subDirSize[i];
print('There are %.3f'%(subDirSize[i]/1024/1024), 'Mbytes in', subdir[i]);
print('There are %.3f' %(wholeDirSize/1024/1024), 'Mbytes in', rootpath);
print('There are %d' %(exceptionCount), 'errors in counting');
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
