[?] sorting files by modification time.

Julius Welby jwelby at waitrose.com
Tue Oct 2 15:48:30 EDT 2001


Get a listing of the file names using os.listdir for the folder, then you
can do something like:

for name in filenames:                             # loop through the names
of files in the folder
    path = os.path.join(folder, name)
    modified = os.path.getmtime(path)     # this is the last modification
date
    statdict[modified] = name                   # add the "last mod" and
"path" to the dictionary as index, value pair
    keylist = statdict.keys()                      # create list of last
updated dates

You can then sort the keylist and create your ordered listing.

I'm sure someone else will post a one liner, but it worked for me when I
used it to find the oldest file in a directory in ovine.py
(www.outwardlynormal.com/python/ovine.htm)

HTH,

Julius


"Francisco Borges" <frandebo at latt.if.usp.br> wrote in message
news:mailman.1002047826.26474.python-list at python.org...
>
>  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???
>
>  thank you for your attention,
> --
>   Francisco.
>              __o
>             `\<,
>      _____(*)/(*)_____
>





More information about the Python-list mailing list