From Python's doc, os.listdir()
returns
a list containing the names of the entries in the directory given by path. The list is in arbitrary order.
What I'm wondering is, is this arbitrary order always the same/deterministic? (from one machine to another, or through time, provided the content of the folder is the same)
Edit: I am not trying to make it deterministic, nor do I want to use this. I was just wondering (for example, what does the order depend on?)
In order to understand what is going on we can inspect the underlying implementation for python 3.2 that can be found here.
We will focus on the POSIX part that starts at line 2574. In the code are defined:
DIR *dirp; // will store the pointer to the directory
struct dirent *ep; // will store the pointer to the entry
There are two important POSIX calls: opendir
at line 2596 and readdir
at line 2611.
As you can read from the readdir
man page:
The readdir() function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dirp. It returns NULL on reaching the end of the directory stream or if an error occurred.
So, readdir
reads the next entry in the directory, but it is up to the file system implementation to define what is the next. You can read more about this topic here:
[...] Because this is a per-filesystem thing, it follows that the traversal order can be different for different directories on the same system even if they have the same entries created in the same order, either because the directories are using different filesystem types or just because some parameters were set differently on the different filesystems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With