Python 来自os.listdir()的非字母数字列表顺序

Python 来自os.listdir()的非字母数字列表顺序,python,list,directory-listing,listdir,Python,List,Directory Listing,Listdir,我经常使用python来处理数据目录。最近,我注意到列表的默认顺序已经变成了一些几乎毫无意义的东西。例如,如果我在包含以下子目录的当前目录中:run01、run02。。。run19、run20,然后我通过以下命令生成一个列表: dir = os.listdir(os.getcwd()) 然后我通常会按以下顺序得到一个列表: dir = ['run01', 'run18', 'run14', 'run13', 'run12', 'run11', 'run08', ... ] 等等。以前的顺序是

我经常使用python来处理数据目录。最近,我注意到列表的默认顺序已经变成了一些几乎毫无意义的东西。例如,如果我在包含以下子目录的当前目录中:run01、run02。。。run19、run20,然后我通过以下命令生成一个列表:

dir = os.listdir(os.getcwd())
然后我通常会按以下顺序得到一个列表:

dir = ['run01', 'run18', 'run14', 'run13', 'run12', 'run11', 'run08', ... ]
等等。以前的顺序是字母数字。但这一新秩序已经伴随我一段时间了


什么决定了这些列表的(显示)顺序?

我认为顺序与文件系统中文件的索引方式有关。
如果你真的想让它遵循某种顺序,你可以在得到文件后对列表进行排序。

这可能就是C的
readdir()
返回的顺序。尝试运行此C程序:

#包括
#包括
内部主(空){
DIR*dirp;
结构方向*de;
dirp=opendir(“.”);
while(de=readdir(dirp))//是,一个“=”。
printf(“%s\n”,de->d_名称);
closedir(dirp);
返回0;
}
构建行应该类似于
gcc-oofoo.c

另外,刚刚运行了这个和您的Python代码,它们都给了我排序输出,所以我无法重现您看到的内容。

os.listdir(路径)

返回列表 包含中项目的名称 路径指定的目录列表 是按任意顺序排列的。事实并非如此 包括特殊条目“.”和 “..”即使他们出现在 目录

顺序不可依赖,是文件系统的产物

In [6]: os.listdir?

Type:       builtin_function_or_method
String Form:<built-in function listdir>
Docstring:
listdir(path) -> list_of_strings
Return a list containing the names of the entries in the directory.
path: path of directory to list
The list is in **arbitrary order**.  It does not include the special
entries '.' and '..' even if they are present in the directory.

要对结果进行排序,请使用
sorted(os.listdir(path))

您可以使用内置的
sorted
函数对字符串进行任意排序。根据你的描述

sorted(os.listdir(whatever_directory))
或者,您可以使用列表的
.sort
方法:

lst = os.listdir(whatever_directory)
lst.sort()
我想我们应该做这个把戏

请注意,
os.listdir
获取文件名的顺序可能完全取决于文件系统。

在[6]:os.listdir中?
In [6]: os.listdir?

Type:       builtin_function_or_method
String Form:<built-in function listdir>
Docstring:
listdir(path) -> list_of_strings
Return a list containing the names of the entries in the directory.
path: path of directory to list
The list is in **arbitrary order**.  It does not include the special
entries '.' and '..' even if they are present in the directory.
类型:内置函数或方法 字符串形式: 文档字符串: listdir(路径)->字符串列表 返回包含目录中条目名称的列表。 路径:要列出的目录的路径 该列表按**任意顺序**。它不包括特别费用 条目“.”和“..”即使它们存在于目录中。
我发现“排序”并不总是符合我的预期。例如,我有一个目录如下,“排序”给了我一个非常奇怪的结果:

>>> os.listdir(pathon)
['2', '3', '4', '5', '403', '404', '407', '408', '410', '411', '412', '413', '414', '415', '416', '472']
>>> sorted([ f for f in os.listdir(pathon)])
['2', '3', '4', '403', '404', '407', '408', '410', '411', '412', '413', '414', '415', '416', '472', '5']

似乎它首先比较第一个字符,如果它是最大的,那么它将是最后一个。

建议将
os.listdir
sorted
命令组合在一起,生成的结果与Linux下的
ls-l
命令相同。以下示例验证了此假设:

user@user-PC:/tmp/test$ touch 3a 4a 5a b c d1 d2 d3 k l p0 p1 p3 q 410a 409a 408a 407a
user@user-PC:/tmp/test$ ls -l
total 0
-rw-rw-r-- 1 user user 0 Feb  15 10:31 3a
-rw-rw-r-- 1 user user 0 Feb  15 10:31 407a
-rw-rw-r-- 1 user user 0 Feb  15 10:31 408a
-rw-rw-r-- 1 user user 0 Feb  15 10:31 409a
-rw-rw-r-- 1 user user 0 Feb  15 10:31 410a
-rw-rw-r-- 1 user user 0 Feb  15 10:31 4a
-rw-rw-r-- 1 user user 0 Feb  15 10:31 5a
-rw-rw-r-- 1 user user 0 Feb  15 10:31 b
-rw-rw-r-- 1 user user 0 Feb  15 10:31 c
-rw-rw-r-- 1 user user 0 Feb  15 10:31 d1
-rw-rw-r-- 1 user user 0 Feb  15 10:31 d2
-rw-rw-r-- 1 user user 0 Feb  15 10:31 d3
-rw-rw-r-- 1 user user 0 Feb  15 10:31 k
-rw-rw-r-- 1 user user 0 Feb  15 10:31 l
-rw-rw-r-- 1 user user 0 Feb  15 10:31 p0
-rw-rw-r-- 1 user user 0 Feb  15 10:31 p1
-rw-rw-r-- 1 user user 0 Feb  15 10:31 p3
-rw-rw-r-- 1 user user 0 Feb  15 10:31 q

user@user-PC:/tmp/test$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.listdir( './' )
['d3', 'k', 'p1', 'b', '410a', '5a', 'l', 'p0', '407a', '409a', '408a', 'd2', '4a', 'p3', '3a', 'q', 'c', 'd1']
>>> sorted( os.listdir( './' ) )
['3a', '407a', '408a', '409a', '410a', '4a', '5a', 'b', 'c', 'd1', 'd2', 'd3', 'k', 'l', 'p0', 'p1', 'p3', 'q']
>>> exit()
user@user-PC:/tmp/test$ 
因此,对于希望在python代码中重现众所周知的
ls-l
命令结果的人来说,
sorted(os.listdir(DIR))
工作得非常好

aaa = ['row_163.pkl', 'row_394.pkl', 'row_679.pkl', 'row_202.pkl', 'row_1449.pkl', 'row_247.pkl', 'row_1353.pkl', 'row_749.pkl', 'row_1293.pkl', 'row_1304.pkl', 'row_78.pkl', 'row_532.pkl', 'row_9.pkl', 'row_1435.pkl']                                                                                                                                                                                                                                                                                                 
sorted(aaa, key=lambda x: int(os.path.splitext(x.split('_')[1])[0]))
与我的需求一样,我在这里有类似于
row_163.pkl
的情况
os.path.splitext('row_163.pkl')
将其分解为
('row_163','.pkl')
,因此也需要基于'\u163'对其进行拆分

但是如果你有要求,你可以这样做

sorted(aa, key = lambda x: (int(re.sub('\D','',x)),x))
在哪里

aa = ['run01', 'run08', 'run11', 'run12', 'run13', 'run14', 'run18']
对于目录检索,您还可以执行排序操作(os.listdir(path))

对于类似
'run01.txt'
'run01.csv'
的情况,您可以这样做

sorted(files, key=lambda x : int(os.path.splitext(x)[0]))

无论出于何种原因,Python都没有内置的方式来进行自然排序(意思是1,2,10而不是1,10,2),因此您必须自己编写:

import re
def sorted_alphanumeric(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)
现在可以使用此函数对列表进行排序:

dirlist = sorted_alphanumeric(os.listdir(...))

问题: 如果您使用上述函数对字符串(例如文件夹名称)进行排序,并希望它们像Windows资源管理器那样进行排序,则在某些边缘情况下,该函数将无法正常工作。
如果文件夹名称中包含某些“特殊”字符,则此排序函数将在Windows上返回不正确的结果。例如,此函数将排序
1!1.a、 一个
,而Windows资源管理器将排序
!1, 1, !a、 a

因此,如果您想像Python中的Windows资源管理器那样进行排序,则必须通过ctypes使用Windows内置函数(这当然在Unix上不起作用):

此函数比排序的字母数字()稍慢。

另外:
winsort
还可以在Windows上对完整路径进行排序

或者,特别是使用Unix时,可以使用
natsort
库(
pip install natsort
)以正确的方式按完整路径进行排序(即子文件夹位于正确的位置)

您可以这样使用它对完整路径进行排序:

from natsort import natsorted, ns
dirlist = natsorted(dirlist, alg=ns.PATH | ns.IGNORECASE)

从7.1.0版开始,natsort支持在内部使用前面提到的Windows API或Linux排序,应该使用natsort()而不是
natsorted()
使用
natsort
库:

对于Ubuntu和其他Debian版本,使用以下命令安装库

Python 2

sudo pip install natsort
Python 3

sudo pip3 install natsort
可以找到有关如何使用此库的详细信息

从natsort导入natsorted
文件=['run01'、'run18'、'run14'、'run13'、'run12'、'run11'、'run08']
已排序(文件)
[out]:
['run01'、'run08'、'run11'、'run12'、'run13'、'run14'、'run18']
  • 这不是一个复制品<代码>natsort在2020年1月27日被添加为一个

我认为默认情况下,顺序由ASCII值确定。这个问题的解决办法是

dir = sorted(os.listdir(os.getcwd()), key=len)
从:

列表按任意顺序排列,不包括特殊条目“.”和“..”,即使它们是
files = list(os.popen("ls"))
files = [file.strip("\n") for file in files]