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

Zentrader zentraders at gmail.com
Wed Aug 15 00:46:23 CEST 2007


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".




More information about the Python-list mailing list