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