In the os.listdir()
function when I try to get my files it sorts all my files automatically without me giving any sort function.
Is there any way to get the default output without sorted one?
In the os.listdir()
function when I try to get my files it sorts all my files automatically without me giving any sort function.
Is there any way to get the default output without sorted one?
Copyright © 2021 Jogjafile Inc.
The documentation says:
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 theos
module itself, it just forwards the corresponding function from either thent
or theposix
module, depending on which OS you are using.As an example, you can see the source code of the POSIX version here:
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:
(I can't find the Windows version right now.)