[SOLVED] How does scandir order files? (Python module os)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So, I'm using python os module, the scandir function and the order of the output doesn't make sense to me. It's not in alphabetical order, as I'd expect, but seemingly completely random.
Basically, it's just simple straight forward
for i in scandir( path ):
print( i )
Is it random? Or is there some order that I'm not seeing? (Like inode order or something)
So, I'm using python os module, the scandir function and the order of the output doesn't make sense to me. It's not in alphabetical order, as I'd expect, but seemingly completely random.
Probably physical order in the directory, compare the output with "ls -f" or "ls -U":
Code:
-f do not sort, enable -aU, disable -ls --color
-U do not sort; list entries in directory order
The sort you normally see is done by ls internally, that's why on large directories it takes a while as it first has to read all entries and then sort them.
BTW: directory order is fs-dependant, tree-oriented fs'es do it different from i.e. ext? which is strictly sequential, new names get put into the holes, left by removed files (there is NO re-sequencing at removes).
yes, it is something like order of creation, but as it was mentioned "new names get put into the holes, left by removed files", moving/renaming files and other things (even just editing/saving) may alter the order.
From the other hand this python for loop will just walk thru the list returned by scandir (which is the "native" order of entries) without any kind of sorting.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.