python 按照自然数排序遍历文件 python os.listdir sort by natural sorting
import os
import re
def sorted_aphanumeric(data):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(data, key=alphanum_key) file = sorted_aphanumeric(os.listdir("./2"))
for f in file:
print(f)


参考
https://stackoverflow.com/questions/4813061/non-alphanumeric-list-order-from-os-listdir
Python for whatever reason does not come with a built-in way to have natural sorting (meaning 1, 2, 10 instead of 1, 10, 2), so you have to write it yourself:
import re
def sorted_aphanumeric(data):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(data, key=alphanum_key)
You can now use this function to sort a list:
dirlist = sorted_aphanumeric(os.listdir(...))
python 按照自然数排序遍历文件 python os.listdir sort by natural sorting的更多相关文章
- Python基础-1 python由来 Python安装入门 注释 pyc文件 python变量 获取用户输入 流程控制if while
1.Python由来 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚 ...
- Python学习笔记之读取文件、OS模块、异常处理、with as语法示例
转:https://m.sogou.com/web/id=4c468b90-3f64-418c-acf8-990b5fe2a757/keyword=python%20os%E6%A8%A1%E5%9D ...
- python入门之排序,文件操作
排序 li.sort() 对li列表从小到大排序,直接更新li列表 sorted(li) 返回一个li排序后的列表,并非直接对li作更新 列表元素必须是同一种数据类型 文件操作 打开文件: f = o ...
- python基础===八大排序算法的 Python 实现
本文用Python实现了插入排序.希尔排序.冒泡排序.快速排序.直接选择排序.堆排序.归并排序.基数排序. 1.插入排序 描述 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一 ...
- 【Python】常用排序算法的python实现和性能分析
作者:waterxi 原文链接 背景 一年一度的换工作高峰又到了,HR大概每天都塞几份简历过来,基本上一天安排两个面试的话,当天就只能加班干活了.趁着面试别人的机会,自己也把一些基础算法和一些面试题整 ...
- c#调用python脚本实现排序(适用于python脚本中不包含第三方模块的情况)
引用:https://www.cnblogs.com/zoe-yan/p/10374757.html 利用vs2017c#调用python脚本需要安装IronPython.我是通过vs2017的工具- ...
- python中os.listdir( )函数读取文件夹
编写pytohn脚本时通常需要批处理. 列出指定目录下的所有文件/文件夹 os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表,但有个很明显的缺点,它的默认顺序不是有序的或 ...
- 【转发】Python使用openpyxl读写excel文件
Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...
- 【Python让生活更美好01】os与shutil模块的常用方法总结
Python作为一种解释型的高级语言,脚本语言,又被称作“胶水语言”,就是因为其灵活的语法和其依靠浩如烟海的第三方包实现的丰富多彩的功能,而os和shutil就是这样一种功能强大的模块,可以非常快捷地 ...
随机推荐
- IIS配置,权限
2. cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319 3. aspnet_regiis.exe -i Chen 19:04:42 %wind ...
- kvm 给虚机增加网卡
[root@666 ok]# virsh domiflist c03 Interface Type Source Model MAC --------------------------------- ...
- tengine 增加ngx_http_cache_purge_module 模块
wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz tar zxvf ngx_cache_purge-2.1.tar.gz -- ...
- yum 完全卸载依赖
实例:安装rabbitmq-server # yum history list rabbitmq-server Loaded plugins: fastestmirror ID | Login use ...
- zabbix 对网卡的流量的监控
新建Template:Network incoming or outcoming on eth1 新建items:Network incoming on eth1 特别注意:储存值:差量(每秒速率)- ...
- Machine Learning Library (MLlib) Guide, BOOKS
download.microsoft.com/download/0/9/6/096170E9-23A2.../9780735698178.pdf Microsoft Azure Essential ...
- uWSGI listen queue 队列溢出的问题
如果没有设置uwsgi的--listen,如果sysctl -a | grep net.core.somaxconn发现net.core.somaxconn=128. 那你使用uwsgi启动的服务,单 ...
- Using curl to upload POST data with files
https://stackoverflow.com/questions/12667797/using-curl-to-upload-post-data-with-files ************* ...
- [转]PLSQL Developer软件使用大全
原文地址:https://www.cnblogs.com/lhrbest/p/6493218.html 第一章 PLSQL Developer特性 PL/SQL Developer是一个集成开发环境, ...
- Go语言学习(四)经常使用类型介绍
1.布尔类型 var v1 bool v1 = true; v2 := (1==2) // v2也会被推导为bool类型 2.整型 类 型 长度(字节) 值 范 围 int8 1 128 ~ 12 ...