Downloading and Uploading a File via FTP
Explore the process of downloading and uploading files using Python's ftplib. Understand how to navigate FTP directories, retrieve single or multiple files, and upload text or binary files with appropriate methods. This lesson equips you with practical skills for managing files on an FTP server.
Downloading a file via FTP
Just viewing what’s on an FTP server isn’t all that useful. We will always want to download a file from the server.
How to download a single file?
Let’s find out how to download a single file:
from ftplib import FTP
ftp = FTP('ftp.debian.org')
print(ftp.login())
#'230 Login successful.'
print(ftp.cwd('debian') )
#'250 Directory successfully changed.'
out = 'README'
with open(out, 'wb') as f:
ftp.retrbinary('RETR ' + 'README.html', f.write)For this example, we login to the Debian Linux FTP and change to the debian folder.
Then we create the name of the file we want to save and open it in write-binary mode. Finally we use the ftp object’s
retrbinary to call RETR to retrieve the file and write it to our
local disk.
How to download all the files?
If you’d like to download all the files, then we’ll ...