Download files from FTP

Load necessary modules

import ftplib
import os

Create ftp object

ftp = ftplib.FTP()

Connect to FTP server

ftp.connect('ftp_server_ip', 'ftp_server_port')

Login to ftp

ftp.login('ftp_username', 'ftp_password')

Change directory to files location

ftp.cwd('path/to/files/on/ftp/server')

Get files list from FTP

for file in ftp.nlst():
	...
The following commands will be inside for block

Store local file path

local_filename = os.path.join(os.path.curdir, 'local_path', file)

Check if target directory exists and create it if not

directory = os.path.dirname(local_filename)
if not os.path.exists(directory):
    os.makedirs(directory)

Write downloaded file on local

lf = open(local_filename, 'w')
ftp.retrbinary("RETR " + file, lf.write, 8*1024)
lf.close()

Final result

#!/usr/bin/env python3

import ftplib
import os

ftp = ftplib.FTP()
ftp.connect('10.1.119.107', 2121)
ftp.login()

# change_directory
ftp.cwd('files/path')

# download_from_directory(self, directory, target):
for file in ftp.nlst():
    print("Downloading {}" . format(file))

    local_filename = os.path.join(os.path.curdir, 'local_path', file)

    # local path must exists
    directory = os.path.dirname(local_filename)
    if not os.path.exists(directory):
        os.makedirs(directory)

    lf = open(local_filename, 'w')
    ftp.retrbinary("RETR " + file, lf.write, 8*1024)
    lf.close()