python 获取文件夹的大小

获取文件夹的大小

import os

def get_size(start_path = '.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            # skip if it is symbolic link
            if not os.path.islink(fp):
                total_size += os.path.getsize(fp)

    return total_size

print(get_size(), 'bytes')

 

print('There are %.3f' % (size / 1024 / 1024), 'Mbytes')\
posted @ 2022-03-20 21:34  bH1pJ  阅读(5)  评论(0编辑  收藏  举报