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'

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM