How to Download and Upload Files in FTP Server using Python

Learn how to use Python's built-in ftplib module to download and upload files in a FTP server using RETR and STOR commands respectively.
Abdou Rockikz · 4 min read · Updated may 2020 · Python Standard Library


One of the main features of FTP server is the ability to store and retrieve files. In this tutorial, you will learn how you can download and upload files in FTP server using Python.

We will be using Python's built-in ftplib module, we gonna use a test FTP server for this tutorial, it is called DLPTEST, let's define its information:

import ftplib

FTP_HOST = "ftp.dlptest.com"
FTP_USER = "dlpuser@dlptest.com"
FTP_PASS = "SzMf7rTE4pCrf9dV286GuNe4N"

The password can change from time to time, make sure you visit their website for the correct credentials, connecting to this server:

# connect to the FTP server
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
# force UTF-8 encoding
ftp.encoding = "utf-8"

Uploading Files

To upload a file, we gonna need to use the ftp.storbinary() method, the below code handles that:

# local file name you want to upload
filename = "some_file.txt"
with open(filename, "rb") as file:
    # use FTP's STOR command to upload the file
    ftp.storbinary(f"STOR {filename}", file)

We opened the file with "rb" mode, which means we're reading the local file in binary mode.

After that, we used the FTP's STOR command, which stores the file in binary mode, it transfer that file on a new port. Note that the file must exist in your local working directory, otherwise this won't work.

This test server will delete the file after 30 minutes, to make sure the file is successfully uploaded, we need to list all files and directories using ftp.dir() method:

# list current files & directories
ftp.dir()

Sure enough, the file is there:

drwxr-xr-x    2 dlptest9   dlptest9        40960 Apr 11 07:04 .
drwxr-xr-x    2 dlptest9   dlptest9        40960 Apr 11 07:04 ..
-rw-r--r--    1 dlptest9   dlptest9          172 Apr 11 07:00 357299070163503-2020-04-11-11-59.txt
-rw-r--r--    1 dlptest9   dlptest9          171 Apr 11 07:01 357299070163503-2020-04-11-12-00.txt
-rw-r--r--    1 dlptest9   dlptest9          171 Apr 11 07:02 357299070163503-2020-04-11-12-01.txt
-rw-r--r--    1 dlptest9   dlptest9          171 Apr 11 07:03 357299070163503-2020-04-11-12-02.txt
-rw-r--r--    1 dlptest9   dlptest9           20 Apr 11 07:04 some_file.txt
-rw-r--r--    1 dlptest9   dlptest9           24 Apr 11 07:00 syslogtest_be.txt

Downloading files

Now let's try to download that same file again:

# the name of file you want to download from the FTP server
filename = "some_file.txt"
with open(filename, "wb") as file:
    # use FTP's RETR command to download the file
    ftp.retrbinary(f"RETR {filename}", file.write)

This time, we're opening the local file in "wb" mode, as we're gonna write the file from the server to the local machine.

We're using RETR command, which downloads a copy of a file on the server, we provide the file name we want to download as the first argument to the command, and the server will send a copy of the file to us.

ftp.retrbinary() method takes the method to call when storing the file on the local machine as a second argument.

If you have deleted that file and run the above code, you'll see the file will appear again, we've successfully downloaded the file!

Finally, you got to quit and close the FTP connection:

# quit and close the connection
ftp.quit()

Alright, we are done with the tutorial, I have separated the code of downloading and uploading scripts, check them here.

Finally, if you're a beginner and want to learn Python, I suggest you take Master Python in 5 Online Courses from University of Michigan, in which you'll learn a lot about Python, good luck!

Related: How to List all Files and Directories in FTP Server using Python.

Happy Learning ♥

View Full Code
Sharing is caring!



Read Also





Comment panel

   
Comment system is still in Beta, if you find any bug, please consider contacting us here.