Domanda

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!

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top