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

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

发布时间:2020-05-24 23:46:54 所属栏目:Python 来源:互联网
导读:统计一个目录下的子目录的Size(清理Program Files文件夹神器)

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

#----------------------------------------------------------------
# -*- coding: utf-8 -*-
#!/usr/bin/env python
#----------------------------------------------------------------
#   Author : pcfeng502
#
#   E-Mail : [emailprotected]
#
#   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');

    

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

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

    推荐文章
      热点阅读