编程语言
首页 > 编程语言> > python 统计指定目录大小

python 统计指定目录大小

作者:互联网

import os
#统计指定目录大小
sum = 0
def statistics_dir(dirPath):
    global sum
    #遍历所有文件,判断是否为文件,文件则统计大小并累加,目录则递归调用,最后返回累加结果
    for f in os.listdir(dirPath):
        absPath=os.path.join(dirPath,f)
        if os.path.isfile(absPath):    #判断是否为文件
            sum += os.path.getsize(absPath)
        elif os.path.isdir(absPath):
            statistics_dir(absPath)
    return sum


a_dir=input("请输入需要统计大小的目录的绝对路径: ")
a=statistics_dir(a_dir)

print("统计的大小为:"+str(a)+"字节")
 

聚悟能 发布了7 篇原创文章 · 获赞 3 · 访问量 754 私信 关注

标签:statistics,python,absPath,指定,sum,path,os,目录,dir
来源: https://blog.csdn.net/tttt0611/article/details/104593258