How to Download and Upload Files in FTP Server using Python?
Last Updated :
12 Jan, 2022
Prerequisite: FTP, ftplib
Here, we will learn how to Download and Upload Files in FTP Server Using Python. Before we get started, first we will understand what is FTP.
FTP(File Transfer Protocol)
File Transfer Protocol(FTP) is an application layer protocol that moves files between local and remote file systems. It runs on the top of TCP, like HTTP. To transfer a file, 2 TCP connections are used by FTP in parallel: control connection and data connection.
For uploading and downloading the file, we will use ftplib Module in Python. It is an in-built module in Python.
What is ftplib module?
This module defines the class FTP and a few related items. The FTP class implements the client-side of the FTP protocol. You can use this to write Python programs that perform a variety of automated FTP jobs, such as mirroring other FTP servers.
We will use a test FTP server, it is called DLPTEST and we are going to use the below text file for all operations:
Let's Understand step by step implementation:
- Enter Required Information, the information will be available click here.
Python3
# Import Module
import ftplib
# Fill Required Information
HOSTNAME = "ftp.dlptest.com"
USERNAME = "[email protected]"
PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
Note: Password will change time to time, make sure you visit their website for the correct credentials.
Python3
# Connect FTP Server
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
# force UTF-8 encoding
ftp_server.encoding = "utf-8"
- Upload the File (To upload a file, we will use storbinary() method)
Syntax:
# Store a file in binary transfer mode
storbinary(command, **)
Python3
# Enter File Name with Extension
filename = "gfg.txt"
# Read file in binary mode
with open(filename, "rb") as file:
# Command for Uploading the file "STOR filename"
ftp_server.storbinary(f"STOR {filename}", file)
- Get the list of directories using dir() method. The test server will remove files after 30 minutes.
Python3
# Get list of files
ftp_server.dir()
Output:

- Download the File (To download a file, we will use retrbinary() method.
Python3
# Enter File Name with Extension
filename = "gfg.txt"
# Write file in binary mode
with open(filename, "wb") as file:
# Command for Downloading the file "RETR filename"
ftp_server.retrbinary(f"RETR {filename}", file.write)
- Close the FTP Connection.
Python3
# Display the content of downloaded file
file= open(filename, "r")
print('File Content:', file.read())
# Close the Connection
ftp_server.quit()
Output:
Below is the complete program for uploading the file in FTP Server:
Python3
# Import Module
import ftplib
# Fill Required Information
HOSTNAME = "ftp.dlptest.com"
USERNAME = "[email protected]"
PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
# Connect FTP Server
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
# force UTF-8 encoding
ftp_server.encoding = "utf-8"
# Enter File Name with Extension
filename = "File Name"
# Read file in binary mode
with open(filename, "rb") as file:
# Command for Uploading the file "STOR filename"
ftp_server.storbinary(f"STOR {filename}", file)
# Get list of files
ftp_server.dir()
# Close the Connection
ftp_server.quit()
Output:
Below is the complete program for downloading the file in FTP Server:
Python3
# Import Module
import ftplib
# Fill Required Information
HOSTNAME = "ftp.dlptest.com"
USERNAME = "[email protected]"
PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
# Connect FTP Server
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
# force UTF-8 encoding
ftp_server.encoding = "utf-8"
# Enter File Name with Extension
filename = "gfg.txt"
# Write file in binary mode
with open(filename, "wb") as file:
# Command for Downloading the file "RETR filename"
ftp_server.retrbinary(f"RETR {filename}", file.write)
# Get list of files
ftp_server.dir()
# Display the content of downloaded file
file= open(filename, "r")
print('File Content:', file.read())
# Close the Connection
ftp_server.quit()
Output:
Similar Reads
How To Upload And Download Files From AWS S3 Using Python? Pre-requisite: AWS and S3Amazon Web Services (AWS) offers on-demand cloud services which means it only charges on the services we use (pay-as-you-go pricing). AWS S3 is a cloud storage service from AWS. S3 stands for 'Simple Storage Service. It is scalable, cost-effective, simple, and secure. We gen
3 min read
How to List all Files and Directories in FTP Server using Python? FTP ( File Transfer Protocol ) is set of rules that computer follows to transfer files across computer network. It is TCP/IP based protocol. FTP lets clients share files. FTP is less secure because of files are shared as plain text without any encryption across the network. It is possible using pyt
2 min read
Upload and Download files from Google Drive storage using Python In this article, we are going to see how can we download files from our Google Drive to our PC and upload files from our PC to Google Drive using its API in Python. It is a REST API that allows you to leverage Google Drive storage from within your app or program. So, let's go ahead and write a Pytho
6 min read
Downloading and Uploading Files from Internet The terms downloading and uploading are generally used while browsing the Internet. Receiving data or a file from the Internet on your computer is referred to as "downloading." Uploading refers to the process of sending data or a file from your computer to a remote location on the Internet. Remote S
7 min read
How to Upload Files Using Python Requests Library We are given some files and our task is to upload it using request library of Python. In this article, we're going to discover a way to use the requests library to add files in diverse scenarios, such as uploading unmarried documents, multiple files, and documents with extra form statistics Upload F
3 min read
How to Download Files from Urls With Python Here, we have a task to download files from URLs with Python. In this article, we will see how to download files from URLs using some generally used methods in Python. Download Files from URLs with PythonBelow are the methods to Download files from URLs with Python: Using 'requests' ModuleUsing 'url
2 min read