Turn your code into any language with our Code Converter. It's the ultimate tool for multi-language programming. Start converting now!
One of the main features of an FTP server is the ability to store and retrieve files. In this tutorial, you will learn how you can download and upload files on an 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"
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 the "rb" mode, which means we read the local file in binary mode.
After that, we used the FTP's STOR
command, which stores the file in binary mode, it transfers that file to 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 the 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
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 will 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.
The 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 have to quit and close the FTP connection:
# quit and close the connection
ftp.quit()
Alright, we are done with this quick tutorial. I have separated the code for downloading and uploading scripts; check them here.
Related: How to List all Files and Directories in FTP Server using Python.
Happy Learning ♥
Found the article interesting? You'll love our Python Code Generator! Give AI a chance to do the heavy lifting for you. Check it out!
View Full Code Improve My Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!