Download data from ftp server with Python

Recently, I needed to download the latest version of software from a ftp server then install it into tested machine.

I think there might be someone would need to know this so today I’m gonna share with you how to download data from ftp server with Python.

The following post might be how to install the app automatically.

I.What is ftp server:

You might want to read this but basically ftp server is where you store the files

II.FTPlib in Python:

You might want to go to official doc for ftplib in here

We’re gonna use ftplib to interact with ftp server

III.Real world example:
Below is a sample working ftp server

Image for post
Image for post

The scenario is this, we need to go to that server, find the latest version of file, and download it.

If the latest version of file is already downloaded, no need to download it again.

1.Store the downloaded version name into yaml file:

Sample configuration file where we have coccoc_dev_version


coccoc_dev_version: 79.0.3945.98
admin_password: '123456'
admin_username: admin

Self-made utils for working with yaml files

class YamlUtils:

def __new__(cls):

if not hasattr(cls, 'instance'):
cls.instance = super(YamlUtils, cls).__new__(cls)

return cls.instance

def read_data_from_file(self, file):
with open(file) as stream:
return yaml.safe_load(stream)
except yaml.YAMLError as exc:

def write_data_to_file(self, loaded, file):
with open(file, 'wb') as stream:
return yaml.safe_dump(loaded, stream, default_flow_style=False
, explicit_start=True, allow_unicode=True, encoding='utf-8')
except yaml.YAMLError as exc:

Override latest version of file to yaml:

yaml_made_utils = YamlUtils()
configuration_info = None
configuration_path = '/resources/configurations.yaml'

def get_configurations_info():
if configuration_info is None:
return yaml_made_utils.read_data_from_file(os.getcwd() + configuration_path)
return configuration_info

class CocCocConfigs:
COCCOC_DEV_VERSION = get_configurations_info()['coccoc_dev_version']

def update_coccoc_dev_version(coccoc_version):
data = get_configurations_info()
data['coccoc_dev_version'] = coccoc_version
return yaml_made_utils.write_data_to_file(data, os.getcwd() + configuration_path)

2.Download the latest version of file if it’s not downloaded yet

def download_latest_coccoc_dev_installer():
coccoc_installer_name = "standalone_coccoc_en.exe"
from ftplib import FTP
with FTP('') as ftp:
ftp.login('anonymous', '')
folders_list = ftp.nlst()
coccoc_download_folder_list = get_list_coccoc_installer_dirs(folders_list)
latest_coccoc_download_dir = coccoc_download_folder_list[-1]
from configs.yaml_configs import CocCocConfigs
if CocCocConfigs.COCCOC_DEV_VERSION not in latest_coccoc_download_dir:
ftp.retrbinary("RETR " + coccoc_installer_name,
open(f"C:\\coccoc-dev\\{coccoc_installer_name}", 'wb').write)
print(f"Error download coccoc binary for {coccoc_installer_name}")
return latest_coccoc_download_dir
return "Already latest"

Notice we need to provide username and password along with the ftp host name like:

with FTP('') as ftp:
ftp.login('anonymous', '')

3.Main function to download file, and updated version name into the configurations file:

def main():
coccoc_version = download_latest_coccoc_dev_installer()
if coccoc_version not in "Already latest":

if __name__ == "__main__":

You can checkout the full source code in github in here

Hope this helps.


Notes: If you feel this blog help you and want to show the appreciation, feel free to drop by :

Image for post
Image for post

This will help me to contributing more valued contents.

Originally published at on January 9, 2020.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store