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])
相关推荐
绿炮火3 分钟前
【MATLAB】(一)简介
开发语言·数学建模·matlab
超浪的晨4 分钟前
JavaWeb 进阶:Vue.js 与 Spring Boot 全栈开发实战(Java 开发者视角)
java·开发语言·前端·javascript·vue.js·html·个人开发
她说人狗殊途14 分钟前
Map 集合
java·开发语言·算法
窗户26 分钟前
有限Abel群的结构(3)
python·抽象代数·编程范式
饭来_30 分钟前
Python 中使用 OpenCV 库来捕获摄像头视频流并在窗口中显示
python·opencv
码界筑梦坊33 分钟前
91-基于Spark的空气质量数据分析可视化系统
大数据·python·数据分析·spark·django·numpy·pandas
EndingCoder1 小时前
Three.js + AI:结合 Stable Diffusion 生成纹理贴图
开发语言·前端·javascript·人工智能·stable diffusion·ecmascript·three.js
坐吃山猪1 小时前
GitPython03-项目setup编译
git·python·setup
Chef_Chen1 小时前
从0开始学习R语言--Day62--RE插补
开发语言·学习·r语言
JavaEdge在掘金1 小时前
cursor1.3 重大更新!复刻 claude code!
python