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])
相关推荐
Dxy123931021614 小时前
Linux 编译安装 Python 3.12.10(多版本共存,不破坏系统Python)
linux·运维·python
神罚天下ET15 小时前
c# ACME client (补充)
开发语言·c#
小灰灰搞电子15 小时前
Qt 实现魔法导航菜单源码分享
开发语言·qt
持敬chijing16 小时前
Python概述
开发语言·python
赤羽尾风17 小时前
NumPy快速入门
python·numpy
倒流时光三十年18 小时前
第三阶段 26 · highlight 高亮(返回命中片段)
后端·python·django
lpfasd12318 小时前
编程语言榜单变迁分析
开发语言·后端·scala
名字还没想好☜18 小时前
Go 用 bufio.Scanner 读大文件踩坑:默认 64KB 行上限、Buffer 扩容与按 Token 切分
开发语言·后端·golang·go
久久学姐19 小时前
Python+Playwright+Pytest+BDD,用FSM打造高效测试框架
python·pytest·bdd·playwright·fsm