How to get the last modified file

I have a folder with a bunch of files. Is there any way to select the latest updated file?

For instance:

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 latest xml_file?

+6
source share
1 answer

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 
+12
source

All Articles