[?] sorting files by modification time.

Julius Welby jwelby at waitrose.com
Wed Oct 3 02:24:00 EDT 2001


Oh yes. I only needed to find the oldest file, so for me this was fine.

To 'Doh!' is human. :-)

"Iñigo Serna" <inigoserna at terra.es> wrote in message
news:mailman.1002062407.8789.python-list at python.org...
Hi,

En mar, 2001-10-02 a 21:48, Julius Welby escribió:
> Get a listing of the file names using os.listdir for the folder, then you
> can do something like:
>
> for name in filenames:
>     path = os.path.join(folder, name)
>     modified = os.path.getmtime(path)
>     statdict[modified] = name
>     keylist = statdict.keys()

No, you can't do this. If several files have the same modification time,
last one would overwrite previous ones.


filenames = os.listdir(folder)
statdict = {}
for name in filenames:
    path = os.path.join(folder, name)
    modified = os.path.getmtime(path)
    while statdict.has_key(modified): # this is the trick:
        modified += 0.1 # do not allow repeated keys
    statdict[modified] = name

keylist = statdict.keys()
keylist.sort()

filenames_sorted_bymtime = []
for k in keylist:
    filenames_sorted_bymtime.append(statdict[k])


I use this in lfm (http://www.terra.es/personal7/inigoserna/lfm).

Best regards,

Iñigo Serna








More information about the Python-list mailing list