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 = "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 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 = "dlpuser@dlptest.com"
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 = "dlpuser@dlptest.com"
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:



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

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.

Similar Reads

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....

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....