Python统计文件夹大小

用到知识,os模块,递归调用

根据文件夹路径计算文件夹大小,路径也可以是文件

方式一,自己递归

import os

size = 0


def get_dir_size(path):
    """
    递归调用,统计文件大小
    :param path: 文件夹路径
    :return: 文件字节
    """
    global size
    # 列表显示出文件夹下的所有子文件及子文件夹
    dir_list = os.listdir(path)
    for file in dir_list:
        # 遍历出是不包含前缀的,所以需要拼接完整路径
        f = os.path.join(path, file)
        # 判断遍历出来的是否为文件夹,是文件夹继续递归调用
        if os.path.isdir(f):
            get_dir_size(f)
        else:
            size += os.path.getsize(f)
    return size

    # 有一定功力的人可以用如下三元表达式
    # s= (get_dir_size(os.path.join(path, file)) if os.path.isdir(os.path.join(path, file))  else os.path.getsize(os.path.join(path, file)) for file in dir_list)
    # return sum(s)
    
def show_size(size):
    """
    按照字节的大小展示显示符号 具体是显示字节呢还是kb还是Mb还是Gb
    :param size: 字节
    :return:显示类型
    """
    if size < 1024:
        return str(size) + ' bytes'
    elif 1024 <= size < 1024 * 1024:
        return str(round(size / 1024, 2)) + ' kb'
    elif 1024 * 1024 <= size < 1024 * 1024 * 1024:
        return str(round(size / (1024 * 1024), 2)) + ' Mb'
    else:
        return str(round(size / (1024 * 1024 * 1024), 2)) + ' Gb'


if __name__ == '__main__':
    while True:
        path = input('请输入文件夹路径:').strip()
        if path in ['q', 'Q']:
            break
        if os.path.exists(path):  # 判断路径是否存在
            if os.path.isfile(path):  # 判断是否为文件
                size = os.path.getsize(path)
            else:  # 说明是文件夹
                size = get_dir_size(path)
            # 调用最终显示具体值的函数
            s = show_size(size)
            print(s)
        else:
            print('========该路径不存在,请重新输入========')

方式1 优化

import os


def get_dir_size(path, size=0):
    """
    递归调用,统计文件大小
    :param path: 文件夹路径
    :return: 文件字节
    """

    # 列表显示出文件夹下的所有子文件及子文件夹
    dir_list = os.listdir(path)
    for file in dir_list:
        # 遍历出是不包含前缀的,所以需要拼接完整路径
        f = os.path.join(path, file)
        # 判断遍历出来的是否为文件夹,是文件夹继续递归调用
        if os.path.isfile(f):
            size += os.path.getsize(f)
        # 否则它就是文件夹,继续递归调用
        else:
            size = get_dir_size(f, size)
    return size

    # 有一定功力的人可以用如下三元表达式
    # s= (get_dir_size(os.path.join(path, file)) if os.path.isdir(os.path.join(path, file))  else os.path.getsize(os.path.join(path, file)) for file in dir_list)
    # return sum(s)


def show_size(size):
    """
    按照字节的大小展示显示符号 具体是显示字节呢还是kb还是Mb还是Gb
    :param size: 字节
    :return:显示类型
    """
    if size < 1024:
        return str(size) + ' bytes'
    elif 1024 <= size < 1024 * 1024:
        return str(round(size / 1024, 2)) + ' kb'
    elif 1024 * 1024 <= size < 1024 * 1024 * 1024:
        return str(round(size / (1024 * 1024), 2)) + ' Mb'
    else:
        return str(round(size / (1024 * 1024 * 1024), 2)) + ' Gb'


if __name__ == '__main__':
    while True:
        path = input('请输入文件夹路径:').strip()
        if path in ['q', 'Q']:
            break
        if os.path.exists(path):  # 判断路径是否存在
            if os.path.isfile(path):  # 判断是否为文件
                size = os.path.getsize(path)
            else:  # 说明是文件夹
                size = get_dir_size(path)
            # 调用最终显示具体值的函数
            s = show_size(size)
            print(s)
        else:
            print('========该路径不存在,请重新输入========')

方式二、使用现成功能

以下只是部分核心代码

import os


def get_dir_size(path, size=0):
    for root, dirs, files in os.walk(path):
        for f in files:
            size += os.path.getsize(os.path.join(root, f))
            print(f)
    return size


s = get_dir_size(path=r'D:\测试\a.txt')
print(s)

你可能感兴趣的:(Python从入门到放弃,Python模块与包,Python函数,python,os模块,递归,三元表达式)