python 按照文件大小读取文件

返回一个list,每个list里面是一个元组(filename, file_size),按照file_size从小到大排序的

python 复制代码
import os

def get_sorted_files(dir_path):
    # 存储最后的文件路径
    files = []
    # 便利dir_path下面的文件或者文件夹
    for file in os.listdir(dir_path):

        file_path = os.path.join(dir_path, file)

        if os.path.isfile(file_path):
            file_size = os.path.getsize(file_path)
            files.append((file, file_size))

    sorted_files = sorted(files, key = lambda x : x[1])

    return sorted_files

sorted_files = get_sorted_files('你的文件路径')
for file in sorted_files:
    print(file[0], file[1])
相关推荐
databook1 天前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室1 天前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三1 天前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户2519162427111 天前
Python之语言特点
python
刘立军1 天前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机1 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i1 天前
django中的FBV 和 CBV
python·django
c8i1 天前
python中的闭包和装饰器
python
这里有鱼汤2 天前
小白必看:QMT里的miniQMT入门教程
后端·python