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])
相关推荐
梯度下降者17 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
MSTcheng.17 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
乌暮18 小时前
少写重复 CRUD:用大白话搞懂 MyBatis-Plus
java·开发语言·spring boot·面试·mybatis
不绝19118 小时前
Lua基础语法入门:注释、变量类型与字符串操作
开发语言·lua
the局外人18 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__18 小时前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python
Hhy_110718 小时前
《C++深度解构07》手撕 vector —— 从底层三指针架构到迭代器失效彻底吃透
c语言·开发语言·c++·学习·visual studio
风落青芜18 小时前
链式二叉树的实现与基本操作
c语言·开发语言·数据结构·算法
艾莉丝努力练剑19 小时前
【QT】系统相关:TCP的API介绍
开发语言·前端·c++·人工智能·qt·学习·tcp/ip
用户03321266636719 小时前
使用 Python 轻松移除 Excel 文档中的重复行
python·excel