LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2019, 10:22 PM   #1
hedron
Member
 
Registered: Jul 2009
Location: NYC
Distribution: Slackware64-multilib 15.0, SARPI, artix
Posts: 401

Rep: Reputation: 32
How does scandir order files? (Python module os)


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)
 
Old 02-10-2019, 11:46 PM   #2
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by hedron View Post
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).
 
1 members found this post helpful.
Old 02-11-2019, 02:31 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Logi Sales Manager on Ncurses (invoice, invoicing, orders, order, sale order, sales order...)? Xeratul Linux - Software 0 03-25-2017 02:45 PM
how to implement a c code for listing all the files without using scandir atikna2001 Programming 1 12-14-2008 11:31 AM
torsmo error scandir for /sys/bus/i2c/devices/: No such file or directory KaZiber Linux - General 2 04-17-2005 02:41 AM
Help with scandir() and c++ carmalt Programming 3 12-06-2004 08:53 AM
I need scandir... jplev22 Linux - Software 0 04-10-2004 05:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration