加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-17 17:24:35 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #----------------------------------------------------------------# -*- coding: utf-8 -*-#!/usr/bin/env python#------------------------------

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#----------------------------------------------------------------
# -*- coding: utf-8 -*-
#!/usr/bin/env python
#----------------------------------------------------------------
#   Author : pcfeng502
#
#   E-Mail : [email?protected]
#
#   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 sizen');
    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),rootpath);
    print('There are %d' %(exceptionCount),'errors in counting');

    

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读