Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab most recently modified file

Tags:

python

I have a folder with a bunch of files. Is there a way to select the file that is the most recently updated?

For example:

FTP_FOLDER = os.path.join(os.getcwd(), 'ftp_folder')
xml_files = [file for file in glob.glob(os.path.join(FTP_FOLDER, '*.xml'))]

Now, how to get the most recent xml_file?

like image 353
David542 Avatar asked Feb 06 '13 21:02

David542


1 Answers

Use os.path.getmtime to get the file modification time:

import os
xml_files.sort(key=os.path.getmtime)
print xml_files[-1] # most recent file
like image 108
nneonneo Avatar answered Nov 14 '22 22:11

nneonneo