The python programming language comes with the ftplib module. The module defines the FTP class and a few related items.
In this tutorial, we will learn the following:
- connecting to ftp server
- how to download all files in a directory
- how to list all files in ftp directory
- uploading multiple files
connecting ftp server
The first thing is to import the Python ftplib module. This will make the module available to our program
from ftplib import FTP ftp = FTP('example.com') ftp.login(user='username', passwd='password') ftp.quit()
That is all it takes to connect to ftp server using Python.
On line 1, we import the FTP class from the ftplib module into our program. The FTP class contains definitions.
On line 2, we are creating an instance of the FTP class. It is through this object that we’ll be calling methods defined in the FTP class. The constructor of the FTP class takes in the host name. The other parameters that can be specified are the password, username, port number and the connection timeout
On line 3, we login to the ftp server by calling the login method on our ftp instance. We specify the username and password. if no username is specified, it defaults to ‘anonymous;. If username is ‘anonymous’, then the password defaults to ‘anonymous@’.Lastly, we disconnect from the ftp server by calling ‘quit’.
How to download all files in a directory
Before downloading files in a directory using Python, we first need to get a list of all files in that directory. To achieve that, we will use the
ftp.nlst()function, which returns a list of file names as returned by the
NLSTcommand. Below is the code to download all files in a directory.
from ftplib import FTP import os download_dir = "/path/to/save/files" ftp = FTP('example.com') ftp.login(user='username', passwd='password') filenames = ftp.nlst() for filename in filenames: local_file_name = os.path.join(download_dir, filename) file = open(local_file_name, "wb") ftp.retrbinary('RETR '+ filename, file.write) ftp.quit()
Once connected to the ftp server, we get a list of files present in the current directory on line 6. We then iterate through the list of file names. For each iteration, we create a local file name. This local file name represents the current file to be saved from the server. We then create a file object. The file object will be passed to retrbinary.
ftp.retrbinary('RETR '+ filename, file.write)retrbinary retrieves a file in binary transfer mode. The second parameter passed to the function is the callback function. It is called for each block of data received, with a single bytes argument giving the data block
Uploading multiple files
Uploading files to ftp server can be achieved by using the STOR ftp command. A client issues the STOR command after successfully establishing a connection when it wishes to upload a copy of a local file to the server. The client provides the file name it wishes to use for the upload. If the file already exists on the server, it is replaced by the uploaded file. If the file does not exist, it is created. This command does not affect the contents of the client’s local copy of the file.
from ftplib import FTP ftp = FTP('example.com') ftp.login(user='username', passwd='password') file = open('file_to_upload','rb') ftp.storbinary('STOR <save_file_as>', file) file.close() ftp.quit()
The example above shows how to upload a single file to the ftp server. it can easily be modified to allow uploading multiple files in one ftp session.
from ftplib import FTP ftp = FTP('example.com') ftp.login(user='username', passwd='password') files_to_upload = ['cat.png', 'dog.png'] # Assuming files are in current working directory for f in files_to_upload: file = open(f, 'rb') ftp.storbinary('STOR <save_file_as>', file) file.close() ftp.quit()