3

I am trying to do a function which receives a list of files (absolute paths) and returns the list sorted by the mtime of the files. Note that the argument is a list of files, not a directory path.

Anyone can help me? Thank you in advance.

EDIT

import os

lista = []
path = 'my/custom/path/'
for dirname, dirnames, filenames in os.walk(path):
    for file in filenames:
        filepath = os.path.realpath(os.path.join(dirname, file))
        lista.append(filepath)

This way I get the list (every file in the path and subpaths), now I need to sort it by mtime!

forvas
  • 9,801
  • 7
  • 62
  • 158
  • what did you try so far? – zmo May 05 '14 at 14:42
  • I was only able to manage sorting by mtime if I get a path instead of a list of files (with the function sorted) – forvas May 05 '14 at 14:47
  • can you provide a [SSCCE](http://sscce.org) of what you've tried so that we can start help you out on something real? – zmo May 05 '14 at 14:51

1 Answers1

3

all you want is:

sorted_list = sorted(lista, key=lambda f: os.stat(f).st_mtime)

which will give you the list of files sorted by mtime.

zmo
  • 24,463
  • 4
  • 54
  • 90