os.listdir
Fredrik Lundh
fredrik at pythonware.com
Tue Oct 30 14:43:46 EST 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')
files.sort(mycomparefunction)
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:
http://py-howto.sourceforge.net/sorting/sorting.html
</F>
<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list