info@worthwebscraping.com
(+91) 79841 03276

How to Upload/Download Data on FTP Server using Python

How to Upload/Download Data on FTP Server using Python

Download Python Script

Send download link to:

FTP stands for File Transfer Protocol. It is way to connect two computers and transfer files securely between them. Most businesses uses FTP severs to share and store shared data in a secure way.

FTP servers are aimed to facilitate file transfers across the internet. If you send files using FTP, files are either uploaded or downloaded to the FTP server. When you’re uploading files, the files are transferred from a personal computer to the server. When you are downloading files, the files are transferred from the server to your personal computer.

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, below are the details we need to add to connect to this server:

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, as shown below:

# local file name you want to upload
filename = "music.csv"
with open(filename, "rb") as file:
# use FTP's STOR command to upload the file
ftp.storbinary("STOR music.csv", 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 transfers 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()

Output:

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 = 'music.csv'
with open(filename, "wb") as file:
# use FTP's RETR command to download the file
ftp.retrbinary("RETR music.csv", 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.

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

# quit and close the connection
ftp.quit()

Output: