If the part with the filename is the same everywhere, then the names can simply be compared as strings since the date format also allows comparisons for strings to be used. Therefore, the max() function will return the desired name:
filenames = ["filename_20180325_180109.zip", "filename_20180326_020107.zip"] print(max(filenames)) # -> filename_20180326_020107.zip
If the first part in the file name may differ, then the date in the required format can be explicitly removed from the name for comparison:
import datetime as DT import re def most_recent(filename): m = re.search(r'_(\d{8})_(\d{6})\.zip$', filename) if m: # matched try: dt = DT.datetime.strptime(' '.join(m.groups()), '%Y%m%d %H%M%S') except ValueError: pass else: return dt, filename return DT.datetime.min, filename
Having a key for comparison, it is easy to get the name with the greatest time:
print(max(filenames, key=most_recent)) # -> filename_20180326_020107.zip
For example, what to download to the current directory from the ftp directory specified by dir most recent zip-archive:
import fnmatch import posixpath def download_most_recent_archive(ftp, dir): filename = max(fnmatch.filter(ftp.nlst(dir), '*.zip'), key=most_recent) with open(filename, 'wb') as file: ftp.retrbinary('RETR ' + posixpath.join(dir, filename), file.write)
Example:
import ftplib with ftplib.FTP('ftp.example.com', 'user', 'passwd') as ftp: download_most_recent_archive(ftp, 'archives')