Fredrik Lundh fredrik at pythonware.com
Tue Oct 30 20:43:46 CET 2001

Randy Heiland wrote:

> >>> for file in os.listdir('mydir'):
> ...   print file
> ...
> f100.dat
> f1.dat
> f2.dat
> but would like this:
> f1.dat
> f2.dat
> f100.dat

files = os.listdir('mydir')
for file in files:
    print file

where mycomparefunction could be something like:

def mycomparefunction(a, b):
    return cmp(int(a[1:-4]), int(b[1:-4]))

but probably needs to be a bit more intelligent, in
case your directory contains other files.

for more information on sorting, see the sorting howto:



<!-- (the eff-bot guide to) the python standard library:

More information about the Python-list mailing list