统计一个目录下的子目录的Size(清理Program Files文件夹神器)

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#----------------------------------------------------------------
# -*- 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');