Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python scandir() ordering \ POSIX readdir ordering

I have a basic question about ordering in scandir function. So far I read man pages for POSIX readdir and didn't found specific information about ordering guaranties.

But when I iterate over large directory (which cannot be changed, read only) I observe same results over multiple systems (Fedora 24 and Ubuntu 16.04).

What is the reason of this behaviour? Where I can read more about it?

If I need consisted ordering, should I wrote my own wrapper around POSIX scandir or someone knows existing implementation for python?

like image 931
L-flame Avatar asked Apr 29 '26 04:04

L-flame


1 Answers

Man page for readdir is explicit:

The order in which filenames are read by successive calls to readdir() depends on the filesystem implementation; it is unlikely that the names will be sorted in any fashion.

In most implementations, a directory is a sequential list entries and both readdir and scandir follow the underlying order. If you used similar file systems on different Linux version, and filled the directory in same order, it is likely that readdir also give same order. The order is not random and is absolutely deterministic and reproductible provided you do not add, remove or rename any file (at least once the file system is stable, because some can delay some actions). Simply it is not predictible from the file names.

So if you want a consistent order, you must deal yourself with the ordering.

like image 162
Serge Ballesta Avatar answered Apr 30 '26 16:04

Serge Ballesta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!