Downloading files

In this section, we are going to learn about downloading files from another machine using ftplib. For that, create a get_ftp_files.py script and write the following content in it:

import os
from ftplib import FTP

ftp = FTP('your-ftp-domain-or-ip')
with ftp:
ftp.login('your-username','your-password')
ftp.cwd('/home/student/work/')
files = ftp.nlst()
print(files)
# Print the files
for file in files:
if os.path.isfile(file):
print("Downloading..." + file)
ftp.retrbinary("RETR " + file ,open("/home/student/testing/" + file, 'wb').write)

ftp.close()

Run the script as follows:

student@ubuntu:~/work$ python3 get_ftp_files.py

You should get the following output:

Downloading...hello
Downloading...hello.c
Downloading...sample.txt
Downloading...strip_hello
Downloading...test.py

In the preceding example, we retrieved multiple files from the host by using the ftplib module. First, we mentioned the IP address, username, and password of the other machine. To get all the files from the host, we used the ftp.nlst() function, and to download those files to our computer, we used the ftp.retrbinary() function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset