Skip to content

Instantly share code, notes, and snippets.

@jeansouzak
Last active March 9, 2024 19:33
Show Gist options
  • Save jeansouzak/bb22a19fbe5264fb1094e135196dba96 to your computer and use it in GitHub Desktop.
Save jeansouzak/bb22a19fbe5264fb1094e135196dba96 to your computer and use it in GitHub Desktop.
Download latest FTP file (sync) using Python example with nlst mlsd or mdtm support
import ftplib
import glob
import os

DOWNLOADED_FILES_LOCAL_FOLDER = "downloaded_files/"

def connect(): 
    ftp_client = ftplib.FTP()
    ftp_client.connect('test.rebex.net', 21)
    ftp_client.login('demo','password')
    return ftp_client


def find_last_downloaded_file_name():
    list_of_downloaded_files = glob.glob(DOWNLOADED_FILES_LOCAL_FOLDER + "/*")
    latest_downloaded_file = max(list_of_downloaded_files, key=os.path.getmtime)
    file_name = os.path.basename(latest_downloaded_file)
    return file_name


def fetch_latest_ftp_file(ftp_client):
    #1. if FTP server supports nlst -t command
    file_names = ftp_client.nlst("-t")
    lines = ftp_client.nlst("-t")
    latest_ftp_file = lines[-1]
    
    #2. if FTP Server supports MLSD command
    # entries = list(ftp_client.mlsd())
    # entries.sort(key = lambda entry: entry[1]['modify'], reverse = True)
    # latest_ftp_file = entries[0][0]
    # print(latest_ftp_file)

    #3. if FTP Server supports MDTM command
    # latest_time = None
    # latest_name = None
    # for file_name in file_names:
    #     print("Getting time from file "+ file_name)
    #     remote_time = ftp_client.voidcmd("MDTM " + file_name)
    #     if (latest_time is None) or (remote_time > latest_time):
    #         latest_name = file_name
    #         latest_time = remote_time

    return latest_ftp_file
    

def save_file_from_ftp(ftp_file_name, ftp_client):
    file = open(DOWNLOADED_FILES_FOLDER+ftp_file_name, 'wb')
    print(ftp_file_name, " is downloading...")
    ftp_client.retrbinary('RETR ' + ftp_file_name, file.write)


ftp_client = connect()
latest_downloaded_file = find_last_downloaded_file_name()
latest_ftp_file = fetch_latest_ftp_file(ftp_client)

if (latest_ftp_file == latest_downloaded_file):
    print("There is no new file to download")
else:
    save_file_from_ftp(latest_ftp_file, ftp_client)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment