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])
相关推荐
dev派2 小时前
AI Agent 系统中的常用 Workflow 模式(2) Evaluator-Optimizer模式
python·langchain
前端付豪4 小时前
AI 数学辅导老师项目构想和初始化
前端·后端·python
用户0332126663674 小时前
将 PDF 文档转换为图片【Python 教程】
python
悟空爬虫5 小时前
UV实战教程,我啥要从Anaconda切换到uv来管理包?
python
dev派5 小时前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
明月_清风7 小时前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽17 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
用户83562907805121 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon1 天前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly1 天前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent