LinuxQuestions.org
Help answer threads with 0 replies.
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, 11:22 PM   #1
hedron
Member
 
Registered: Jul 2009
Location: NYC
Distribution: Slackware64-multilib 14.2, SARPI
Posts: 307

Rep: Reputation: 27
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-11-2019, 12:46 AM   #2
ehartman
Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 362

Rep: Reputation: 160Reputation: 160
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, 03:31 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,009

Rep: Reputation: 3687Reputation: 3687Reputation: 3687Reputation: 3687Reputation: 3687Reputation: 3687Reputation: 3687Reputation: 3687Reputation: 3687Reputation: 3687Reputation: 3687
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03:45 PM
how to implement a c code for listing all the files without using scandir atikna2001 Programming 1 12-14-2008 12:31 PM
torsmo error scandir for /sys/bus/i2c/devices/: No such file or directory KaZiber Linux - General 2 04-17-2005 03:41 AM
Help with scandir() and c++ carmalt Programming 3 12-06-2004 09:53 AM
I need scandir... jplev22 Linux - Software 0 04-10-2004 06:51 PM

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

All times are GMT -5. The time now is 10:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration