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])
相关推荐
宋拾壹12 小时前
同时添加多个类目
android·开发语言·javascript
IT知识分享12 小时前
从零开发在线简繁转换工具:OpenCC 实战、避坑经验与方案选型
javascript·python
lunzi_082612 小时前
【学习笔记】《Python编程 从入门到实践》第8章:函数定义、参数传递与模块导入
笔记·python·学习
凡人叶枫12 小时前
Effective C++ 条款04:确定对象被使用前已先被初始化
java·linux·开发语言·c++·嵌入式开发
杨运交12 小时前
[030][Web模块]Spring Boot 验证与 OpenAPI 集成实战:从校验规则到文档生成
前端·spring boot·python
培培说证12 小时前
2026财务岗位如何快速提升自身能力
python
小小龙学IT12 小时前
Go 语言后端开发:从并发模型到生产落地的工程实践
开发语言·后端·golang
努力攻坚操作系统12 小时前
编程语言编译运行机制对比:C / Java / Python
java·c语言·python
godspeed_lucip12 小时前
LLM和Agent——专题6:Multi Agent 入门(5)
人工智能·python
ytttr87313 小时前
Qt 数字键盘实现
开发语言·qt