In this tutorial we will explore how to work with FTP server using Python.

Table of Contents


Introduction

FTP (File Transfer Protocol) is a set of rules describing how the files are transferred within a computer network.

Many companies using FTP to securely send large files across the organization.

For programmers it is often a requirement to send output files such as model predictions or test results using FTP so the files can be retrieved by the stakeholders across different business units.

In this tutorial we will use a public FTP test server DLP Test that offers free FTP server for testing purposes and automatically deletes the files after a few minutes.


Connect to FTP server using Python

The first step to start working with FTP server using Python is to connect to the FTP server.

Let’s import the required dependency:


import ftplib

In order to connect to the FTP server, you will need to know some credentials:

  • Host
  • Username
  • Password

The credentials to the test server are available here.

Let’s define the credentials as Python variables:


#Define credentials

FTP_HOST = "ftp.dlptest.com"
FTP_USER = "dlpuser"
FTP_PASS = "rNrKYTX9g7z3RgJRmxWuGHbeu"

And finally establish a connection to the FTP server using Python:


#Connect to FTP server

ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)

Now the connection is established and we can verify it by requesting a welcome message from the FTP server:


#Get welcome message

print(ftp.getwelcome())

which should return:

'220 Welcome to the DLP Test FTP Server'

Note: not all FTP servers have welcome messages, in case of the DLP Test FTP server, a welcome message should be displayed.


List files in the FTP server using Python

One of the simplest operations with FTP server is to retrieve a list of files in the directory.

Currently we are in the root directory and it can be verified by running:


#Get current directory path

print(ftp.pwd())

which should return:

/

Now we can get a list of files in the current directory. There are two ways of doing it:

  • Produce a directory listing which prints it to standard output
  • Return a list of file names

Produce a directory listing

Simply run:


#Produce a directory listing

ftp.dir()

which should print a standard output with something like:

-rw-r--r--    1 1001     1001          958 Aug 15 17:30 file1.txt
-rw-r--r--    1 1001     1001          958 Aug 15 17:30 file2.txt
-rw-r--r--    1 1001     1001            0 Aug 15 17:31 file3.txt

Return a list of file names in a directory

Simply run:


#Produce a list of files in the directory

print(ftp.nlst())

which should return a Python list like:

['file1.txt', 'file2.txt', 'file3.txt']

Note: the filenames returned by both commands will be identical.


Create a new directory in the FTP server using Python

Before we start testing uploading files to FTP, let’s create our own folder for these test cases.

Since this FTP server is used by many developers to test their FTP code, we will create a subdirectory so it’s easier to follow the tutorial.

Our folder will be called ‘pyshark’.

Simply run:


#Create a new folder in the directory

ftp.mkd('pyshark')

and the folder will be created.

You can verify it either by connecting to FTP server using FileZilla or my printing a list of file names in the directory, and one of them will be ‘pyshark’, which is the folder that we have just created.


Next, lets set this new directory as the current directory on the server by running:


#Set the new folder as the current directory

ftp.cwd('pyshark')

and to validate the change, print the name of the current directory:


#Get current directory path

print(ftp.pwd())

and you should get:

/pyshark

Sample file

In order to continue in this tutorial we will need some sample file to work with.

We will create a sample TXT file with test: “This is a sample file.”


Upload files to FTP server

In this section we will explore how to upload a file to the FTP server.

So far we have create a new folder in FTP called “pyshark” and set it as the current working directory.

In order to upload the file to FTP server, we will use the .storbinary() method of the FTP class. This method initiates a file transfer from FTP client to FTP server using the FTP command STOR.

First we will open the sample file “file1.txt” as a binary file with read mode and then perform the FTP file transfer (we will name the transferred file on the server as “uploaded_file”):


#Upload file to FTP server

with open('file1.txt', 'rb') as f:
    ftp.storbinary('STOR ' + 'uploaded_file.txt', f)

To check if it was uploaded simply produce a list of files in the directory:


#Produce a list of files in the directory

print(ftp.nlst())

And you should get:

['uploaded_file.txt']

Download files from FTP server

In this section we will explore how to download a file from the FTP server.

We continue to work with “pyshark” folder as the current working directory.

In order to download a file from FTP server, we will use the .retrbinary() method of the FTP class. This method retrieves a file from the FTP server to the local system in binary mode using the FTP command RETR.

We are going to download the file that we uploaded to the FTP server in the previous section.

First we will create a file object “downloaded_file.txt” in as a binary file with write mode and then download the “uploaded_file.txt” from the FTP server:


#Download file from FTP server

with open('downloaded_file.txt', 'wb') as f:
    ftp.retrbinary('RETR ' + 'uploaded_file.txt', f.write)

The “downloaded_file.txt” should appear in the same directory as the file with the code:


Rename files in FTP server

In this section we will explore how to rename a file in the FTP server.

We continue to work with “pyshark” folder as the current working directory.

In order to delete a file from FTP server, we will use the .delete() method of the FTP class. This method takes two parameter which are the original file name and the new file name.

Let’s delete the “uploaded_file.txt” to “renamed_file.txt” in the FTP server:


#Rename file in FTP server

ftp.rename('uploaded_file.txt', 'renamed_file.txt')

To check if it was uploaded simply produce a list of files in the directory:


#Produce a list of files in the directory

print(ftp.nlst())

And you should get:

['renamed_file.txt']

Delete files from FTP server

In this section we will explore how to delete a file from the FTP server.

We continue to work with “pyshark” folder as the current working directory.

In order to delete a file from FTP server, we will use the .delete() method of the FTP class. This method takes one parameter which is the file name and then deletes it from the directory.

Let’s delete the “uploaded_file.txt” from the FTP server:


#Delete file from FTP server

ftp.delete('uploaded_file.txt')

To check if it was uploaded simply produce a list of files in the directory:


#Produce a list of files in the directory

print(ftp.nlst())

And you should get:

[]

Conclusion

In this article we explored how to work with FTP server using Python including uploading, downloading, deleting files from the directory and other useful functions of ftplib.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Programming tutorials.