[?] sorting files by modification time.

Joonas Paalasmaa joonas at olen.to
Tue Oct 2 16:04:05 EDT 2001


Francisco Borges wrote:
> 
>  Hello all,
> 
>   I want to list some files in a directory and sort them by the
>   modification time.
> 
>  As far as I could tell, I can't do that with os.listdir and neither
>  with glob, is this right?
> 
> I thought about
>  >>> os.system('ls -t')
> 
> but it will thrown the output to stdout. Is there a simpler way to do
> this than start messing with the stdout or with the shell???

Take a look at Python sorting FAQ at
http://py-howto.sourceforge.net/sorting/sorting.html .

>>> import os
>>> files = os.listdir(".")
>>> files.sort(lambda x,y:cmp(os.path.getmtime(x),os.path.getmtime(y)))



More information about the Python-list mailing list