The Python Oracle

python listing dirs in a different order based upon platform

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Question
02:03 Accepted answer (Score 13)
02:44 Answer 2 (Score 14)
02:56 Thank you

--

Full question
https://stackoverflow.com/questions/5667...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #oswalk

#avk47



ANSWER 1

Score 14


for path, dirs, files in os.walk('d:\ssptemp'):

    # sort dirs and files
    dirs.sort()
    files.sort()

    print "parsing dir(s)"
    # ...



ACCEPTED ANSWER

Score 13


The order of directories within os.walk is not necessarily alphabetical (I think it's actually dependent upon how they're stored within the dirent on the filesystem). It will likely be stable on the same exact directory (on the same filesystem) if you don't change the directory contents (ie, repeated calls will return the same order), but the order is not necessarily alphabetical.

If you want to have an ordered list of filenames you will have to build the list and then sort it yourself.