简体   繁体   English

在 os.listdir() 中取消排序文件

[英]Unsorting files in os.listdir()

In the os.listdir() function when I try to get my files it sorts all my files automatically without me giving any sort function.os.listdir()函数中,当我尝试获取我的文件时,它会自动对所有文件进行排序,而无需我提供任何排序​​功能。

Is there any way to get the default output without sorted one?有没有办法在没有排序的情况下获得默认输出?

The documentation says:文档说:

The list is in arbitrary order列表的顺序是任意的

There is no way to "unsort" this.没有办法“取消排序”这个。 That it appears to be sorted in your case is coincidence, it might as well not be.它似乎在您的情况下排序是巧合,也可能不是。

In case of CPython, the listdir function is not even defined in the os module itself, it just forwards the corresponding function from either the nt or the posix module, depending on which OS you are using.在 CPython 的情况下, listdir函数甚至没有在os模块本身中定义,它只是从ntposix模块转发相应的函数,具体取决于您使用的操作系统。

As an example, you can see the source code of the POSIX version here:例如,您可以在此处查看 POSIX 版本的源代码:

https://github.com/python/cpython/blob/5efb1a77e75648012f8b52960c8637fc296a5c6d/Modules/posixmodule.c#L3739 https://github.com/python/cpython/blob/5efb1a77e75648012f8b52960c8637fc296a5c6d/Modules/posixmodule.c#L3739

The order in which the entries are added to the result list is determined by calling the readdir function repeatedly, which does not specify any order, either:条目添加到结果列表的顺序是通过重复调用readdir函数来确定的,该函数也没有指定任何顺序:

The order in which filenames are read by successive calls to readdir() depends on the filesystem implementation;连续调用 readdir() 读取文件名的顺序取决于文件系统的实现; it is unlikely that the names will be sorted in any fashion.不太可能以任何方式对名称进行排序。


(I can't find the Windows version right now.) (我现在找不到 Windows 版本。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM