There are files with a type name: "filename_20180325_180109.zip, filename_20180326_020107.zip", i.e. differ only in numbers at the end of the name, which is the date of creation. It is also important that the number consists of two parts and for me only the first part, which is before the "_", matters. You need to download only one file with a large number at the end of the name (with the last date) and put it next to the script.

Probably there are ideas how to fasten the fnmatch module to a solution of a problem

  • Download exactly one file with a large number in the name - Dmitriy Vladimirovich
  • it turns out that step 2 - Dmitry Vladimirovich
  • greater value in the second file. those. later date (2018.03.26) - Dmitry Vladimirovich
  • Yes, I understand you, this approach to the formulation of the question is more useful, thank you! - Dmitry Vladimirovich

1 answer 1

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')