...

/

Downloading and Uploading a File via FTP

Downloading and Uploading a File via FTP

Here we'll see how we can download and upload a file using FTP.

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)
Downloading one file

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 ...