Python FTP get the most recent file by date Python FTP get the most recent file by date linux linux

Python FTP get the most recent file by date


For those looking for a full solution for finding the latest file in a folder:

MLSD

If your FTP server supports MLSD command, a solution is easy:

entries = list(ftp.mlsd())entries.sort(key = lambda entry: entry[1]['modify'], reverse = True)latest_name = entries[0][0]print(latest_name)

LIST

If you need to rely on an obsolete LIST command, you have to parse a proprietary listing it returns.

Common *nix listing is like:

-rw-r--r-- 1 user group           4467 Mar 27  2018 file1.zip-rw-r--r-- 1 user group         124529 Jun 18 15:31 file2.zip

With a listing like this, this code will do:

from dateutil import parser# ...lines = []ftp.dir("", lines.append)latest_time = Nonelatest_name = Nonefor line in lines:    tokens = line.split(maxsplit = 9)    time_str = tokens[5] + " " + tokens[6] + " " + tokens[7]    time = parser.parse(time_str)    if (latest_time is None) or (time > latest_time):        latest_name = tokens[8]        latest_time = timeprint(latest_name)

This is a rather fragile approach.


MDTM

A more reliable, but a way less efficient, is to use MDTM command to retrieve timestamps of individual files/folders:

names = ftp.nlst()latest_time = Nonelatest_name = Nonefor name in names:    time = ftp.voidcmd("MDTM " + name)    if (latest_time is None) or (time > latest_time):        latest_name = name        latest_time = timeprint(latest_name)

For an alternative version of the code, see the answer by @Paulo.


Non-standard -t switch

Some FTP servers support a proprietary non-standard -t switch for NLST (or LIST) command.

lines = ftp.nlst("-t")latest_name = lines[-1]

See How to get files in FTP folder sorted by modification time.


Downloading found file

No matter what approach you use, once you have the latest_name, you download it as any other file:

with open(latest_name, 'wb') as f:    ftp.retrbinary('RETR '+ latest_name, f.write)

See also


Why don't you use next dir option?

ftp.dir('-t',data.append)

With this option the file listing is time ordered from newest to oldest. Then just retrieve the first file in the list to download it.


With NLST, like shown in Martin Prikryl's response,you should use sorted method:

ftp = FTP(host="127.0.0.1", user="u",passwd="p")ftp.cwd("/data")file_name = sorted(ftp.nlst(), key=lambda x: ftp.voidcmd(f"MDTM {x}"))[-1]