In this article, we provide a simple Python script to download files from an FTP server using Python.
Write the FTP address and your username and password in the place specified below. Save this file in Python format (for example, download.py) and run it as:
$ python3 download.py
#!/usr/bin/env python3
import fnmatch
from ftplib import FTP
ftp = FTP('ftp address')
#if no username and password is required.
ftp.login()
#if username and password is required.
ftp.login('your-username','your-password')
# if you have to change directory on FTP server.
ftp.cwd('/path/to/dir/')
# Get all files
files = ftp.nlst()
# Download files
for file in files:
if fnmatch.fnmatch(file, '*.tar.gz'): #To download specific files.
print("Downloading..." + file)
try:
ftp.retrbinary("RETR " + file ,open("/path/to/dir/on/your/local/system/" + file, 'wb').write)
except EOFError: # To avoid EOF errors.
pass
ftp.close()