Python: get the newest file in a dir
I need to get the newest file in a directory:
import os
def get_newest_file(extns):
filelist = os.listdir(os.getcwd())
filelist = filter(lambda x: not os.path.isdir(x) and extns in x,
filelist)
return max(filelist, key=lambda x: os.stat(x).st_mtime)
The applied filter excludes subdirs “and” file with different extension than that passed as argument.
>>> get_newest_file('.log')
'L0403002.log'
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Devo ottenere il file più recente all’interno di una directory, in base
all’estensione:
import os
def get_newest_file(extns):
filelist = os.listdir(os.getcwd())
filelist = filter(lambda x: not os.path.isdir(x) and extns in x,
filelist)
return max(filelist, key=lambda x: os.stat(x).st_mtime)
Il filtro applicato esclude le sottodirectory e i file con estensione diversa da quella passata come argomento.
>>> get_newest_file('.log')
'L0403002.log'
Categorie: python
Commenti (0)
Trackback (0)
Lascia un commento
Trackback
Commenti recenti