os.listdir

Emile van Sebille emile at fenx.com
Tue Oct 30 12:47:34 EST 2001


"Randy Heiland" <heiland at ncsa.uiuc.edu> wrote in message
news:3BDEE6C6.8E96DBF3 at ncsa.uiuc.edu...
> I get this:
>
> >>> for file in os.listdir('mydir'):
> ...   print file
> ...
> f100.dat
> f1.dat
> f2.dat
>
> but would like this:
> f1.dat
> f2.dat
> f100.dat
>
> What's the trick?
>

You mean, besides putting the files out there with sort-appropriate names?
I'd build a dictionary of names specific to the requirements then sort it.
eg, make it look like:

>>> d = {('f',1):'f1.dat', ('f',2):'f2.dat', ('f',100):'f100.dat'}

then:

>>> ds = d.items()
>>> ds.sort()
>>> for k,d in ds: print d

f1.dat
f2.dat
f100.dat
>>>


HTH,

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list