python 获取文件夹大小

__author__ = 'bruce'

import os
from os.path import join,getsize

def getdirsize(dir):
    size=0l
    for (root,dirs,files) in os.walk(dir):
        
        for name in files:
            try:
                #print getsize(join(root,name))
                size += getsize(join(root,name))
            except:
                continue

        #直接用下面这句代码,在ubuntu 会出错,在windows 下没测试过。
        #size += sum([getsize(join(root,name)) for name in files])
    return size

if __name__ == '__main__':
    filesize = getdirsize(r'/home/candy/')
    print 'There are %.2f ' %(filesize/1024),'Kb in /home/bruce/Downloads'

  

原文地址:https://www.cnblogs.com/zj1111184556/p/3605603.html