What order does info get returned in by os.listdir()

Steve Holden steve at holdenweb.com
Wed Aug 15 03:41:57 CEST 2007


Zentrader wrote:
> On Aug 14, 1:52 pm, Jeremy C B Nicoll <jer... at blueyonder.co.uk> wrote:
>> What order does info get returned in by os.listdir() ?
>>
>> I'm just starting to write python code, on an Win XP Pro machine.  I've got
>> various directories which, when I look at them in Win XP, sorted by name, I
>> see in order, eg:
>>
>>    ~prefixed file   .txt
>>    A.txt
>>    B.txt
>>    ...
>>    Z.txt
>>
>> ie, XP seems to think that files with a "~" as their first character are
>> sorted ahead of A.
>>
>> When I use os.listdir() to return that list of leaf values, I do seem to get
>> them in alphabetical order, A before B before C etc, but the ~-prefixed ones
>> are returned after the Z-prefixed files rather than before the A-ones.
>>
>> I was wondering why...
>>
>> --
>> Jeremy C B Nicoll - my opinions are my own.
> 
> I think that os.listdir() returns file names in chronological order,
> that is in the order they were created on disk.  If there is any
> sorting done it will be in ASCII order.  All data is stored as a
> number so it is sorted that way.  ord("~")= 126, and ord("Z")=90 so it
> makes sense that "~" is after "Z".
> 
You can think what you like, but reading the documentation is usually a 
more reliable way to resolve uncertainties, and it says:

"""The list is in arbitrary order."""

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list