How to Download and Upload Files in FTP Server using Python?

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.
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.
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 Moduleimport ftplib # Fill Required InformationHOSTNAME = "ftp.dlptest.com"USERNAME = "dlpuser@dlptest.com"PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
Note: Password will change time to time, make sure you visit their website for the correct credentials.
Connect to Server
Python3
# Connect FTP Serverftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD) # force UTF-8 encodingftp_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 Extensionfilename = "gfg.txt" # Read file in binary modewith 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 filesftp_server.dir()
Output: 
Download the File (To download a file, we will use retrbinary() method.
Python3
# Enter File Name with Extensionfilename = "gfg.txt" # Write file in binary modewith 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 filefile= open(filename, "r")print('File Content:', file.read()) # Close the Connectionftp_server.quit()
Output: 
Below is the complete program for uploading the file in FTP Server: 
Python3
# Import Moduleimport ftplib # Fill Required InformationHOSTNAME = "ftp.dlptest.com"USERNAME = "dlpuser@dlptest.com"PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N" # Connect FTP Serverftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD) # force UTF-8 encodingftp_server.encoding = "utf-8" # Enter File Name with Extensionfilename = "File Name" # Read file in binary modewith open(filename, "rb") as file:    # Command for Uploading the file "STOR filename"    ftp_server.storbinary(f"STOR {filename}", file) # Get list of filesftp_server.dir() # Close the Connectionftp_server.quit()
Output: 
Below is the complete program for downloading the file in FTP Server: 
Python3
# Import Moduleimport ftplib # Fill Required InformationHOSTNAME = "ftp.dlptest.com"USERNAME = "dlpuser@dlptest.com"PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N" # Connect FTP Serverftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD) # force UTF-8 encodingftp_server.encoding = "utf-8" # Enter File Name with Extensionfilename = "gfg.txt" # Write file in binary modewith open(filename, "wb") as file:    # Command for Downloading the file "RETR filename"    ftp_server.retrbinary(f"RETR {filename}", file.write) # Get list of filesftp_server.dir() # Display the content of downloaded filefile= open(filename, "r")print('File Content:', file.read()) # Close the Connectionftp_server.quit()
Output:
simmytarika5
Picked
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.