[?] sorting files by modification time.

Iñigo Serna inigoserna at terra.es
Tue Oct 2 18:42:47 EDT 2001


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