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

def wjsl(aae):
    ls=os.listdir(aae)
    return len(ls)

dizhi="d:\\python37\\abc\\"
size=get_size(dizhi)
sum=wjsl(dizhi)
print('%.3f' %(size/1024/1024),'M')
print(str(sum),"个文件")

 

posted @ 2023-01-13 05:50  myrj  阅读(110)  评论(0编辑  收藏  举报