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

J

Jeremy C B Nicoll

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

Zentrader

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

Zentrader

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

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

Steve Holden

Zentrader said:
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 -------------
 
J

Jeremy C B Nicoll

Jeremy C B Nicoll said:
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.

Thanks to people who replied here, and by email.

I've some supplementary questions... my original code was looking at each
leafname in turn via

for leaf in os.listdir(path):
wholefile = os.path.join(path,leaf)
if os.path.isfile(wholefile):
if leaf.startswith("~"):

etc. But I now realise I might alternatively do something like:

leaflist = os.listdir(path)
<then something to sort that list>
for leaf in leaflist:

How would I sort leaflist in a way that mimics the sort order that XP shows
me things under?



Secondly, my code is wasting time looking at subdirectories/files which I
already know I'm not interested in. Is there a way to restrict listdir to,
say, only return info about subdirectories, or only about dirs/files whose
names match a pattern?


Thirdly, once I've go a list of leafnames, somehow, is there a way to ask
Python if a specific leaf-value is in that list, without explicitly looping
through the items in the list?
 
M

Marc 'BlackJack' Rintsch

I've some supplementary questions... my original code was looking at each
leafname in turn via

for leaf in os.listdir(path):
wholefile = os.path.join(path,leaf)
if os.path.isfile(wholefile):
if leaf.startswith("~"):

etc. But I now realise I might alternatively do something like:

leaflist = os.listdir(path)
<then something to sort that list>
for leaf in leaflist:

But this is doing something different that the above code!?
How would I sort leaflist in a way that mimics the sort order that XP shows
me things under?

This depends on what XP is. Which program? Which locale? How does the
locale influence that programs sorting?
Secondly, my code is wasting time looking at subdirectories/files which I
already know I'm not interested in. Is there a way to restrict listdir to,
say, only return info about subdirectories, or only about dirs/files whose
names match a pattern?

`os.listdir()` always returns all names. You can or have to filter the
result if you are only interested in some of the names. Simple pattern
matching on names can be done with `glob.glob()`.
Thirdly, once I've go a list of leafnames, somehow, is there a way to
ask Python if a specific leaf-value is in that list, without explicitly
looping through the items in the list?

With the ``in`` operator you have an implicit loop over the list.

if 'readme.txt' in leafnames:
print 'Hurray!'

Ciao,
Marc 'BlackJack' Rintsch
 
J

Jeremy C B Nicoll

Marc 'BlackJack' Rintsch said:
But this is doing something different that the above code!?

I'm not sure if I understand you. I know it's only "equivalent" to the
first line of what's above, ie iterate over a list of names, and obviously
it's got the sorting of that list done, but do you mean there's some other
difference?


This depends on what XP is. Which program? Which locale? How does the
locale influence that programs sorting?

Well... XP is Windows XP (Pro as I think I said earlier), and I'm in the UK.
I explained earlier how XP shows me stuff in order when I tell it to sort by
name.
`os.listdir()` always returns all names. You can or have to filter the
result if you are only interested in some of the names. Simple pattern
matching on names can be done with `glob.glob()`.


With the ``in`` operator you have an implicit loop over the list.

if 'readme.txt' in leafnames:
print 'Hurray!'

OK, that looks useful. Thanks.
 
J

Jon Clements

To emulate the order of XP, you might be able to get away with
something like:-

sorted( myData, key=lambda L: L.replace('~',chr(0)) )

That just forces all '~'s to be before everything else.

hth,

Jon.
 
M

Miles

Well... XP is Windows XP (Pro as I think I said earlier), and I'm in the UK.
I explained earlier how XP shows me stuff in order when I tell it to sort by
name.

Case insensitive sort with ~ coming before all other characters:
some_list.sort(key=lambda k: k.lower().split("~"))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top