python获取文件及文件夹大小

来源:互联网 发布:excel破解密码软件 编辑:程序博客网 时间:2024/04/20 06:29

@1.获取文件大小

使用os.path.getsize函数,参数是文件的路径。


@2.获取文件夹大小,即遍历文件夹,将所有文件大小加和。遍历文件夹使用os.walk函数

import osfrom os.path import join, getsizedef getdirsize(dir):   size = 0L   for root, dirs, files in os.walk(dir):      size += sum([getsize(join(root, name)) for name in files])   return sizeif '__name__' == '__main__':   filesize = getdirsize(r'c:\windows')   print 'There are %.3f' % (size/1024/1024), 'Mbytes in c:\\windows'


原创粉丝点击