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])
相关推荐
a1117763 小时前
医院挂号预约系统(开源 Fastapi+vue2)
前端·vue.js·python·html5·fastapi
0思必得03 小时前
[Web自动化] Selenium处理iframe和frame
前端·爬虫·python·selenium·自动化·web自动化
virus59453 小时前
悟空CRM mybatis-3.5.3-mapper.dtd错误解决方案
java·开发语言·mybatis
初次见面我叫泰隆3 小时前
Qt——3、常用控件
开发语言·qt·客户端
无小道5 小时前
Qt——QWidget
开发语言·qt
时艰.5 小时前
Java 并发编程之 CAS 与 Atomic 原子操作类
java·开发语言
梵刹古音5 小时前
【C语言】 函数基础与定义
c语言·开发语言·算法
摘星编程5 小时前
OpenHarmony + RN:Calendar日期选择功能
python
梵刹古音5 小时前
【C语言】 结构化编程与选择结构
c语言·开发语言·嵌入式
Yvonne爱编码5 小时前
JAVA数据结构 DAY3-List接口
java·开发语言·windows·python