Use FTP in Python



In Python we can use FTP (File Transfer Protocol) by importing the ftplib module, it will provide a simple interface to access FTP servers to retrieve files and process them locally. ftplib module allows us to write Python programs that perform a variety of automated FTP tasks.

Objectives of FTP

The objectives of FTP are as follows

  • FTP provides file sharing.
  • FTP helps us to encourage the use of remote computers.
  • FTP is used to transfer the data reliably and efficiently.

Some Common tasks we can perform using FTP in Python by utilizing the 'ftplib' module are as follows:

  • Connecting to an FTP Server
  • Downloading a File
  • Uploading a file
  • Printing the File List

Connecting to an FTP Server

The following will connect you to a remote server, then we can change it into a specific directory.

from ftplib import FTP

#domain name or server ip: 
ftp = FTP('123.server.ip')
ftp.login(user='username', passwd = 'password'

While executing the above program to connect the FTP server, if the connection is successful then, it will display Login successful.'

'230 Login successful.'

If the connection fails, due to an incorrect username or password it will display Login incorrect.'

'530 Login incorrect.'

Downloading a File

Firstly, we have to assign the file name to a variable and, open our local file using the open() method. Along with the filename you need to pass the string representing the desired mode. In our scenario the file we need to download is a binary file therefore, the mode will be wb.

Next, we have to retrieve the data which is in binary format from the remote server, then we write to the local file what we find. The last parameter (1024) acts as a reference to buffering.

from ftplib import FTP

def download_file():
    # Establish FTP connection
    ftp = FTP('ftp.example.com')  
    ftp.login('username', 'password')  

    # Define the file to be downloaded
    file_name = 'example.txt'

    # Open a local file in write-binary mode
    with open(file_name, 'wb') as local_file:
        # Download the file from the FTP server
        ftp.retrbinary('RETR ' + file_name, local_file.write, 1024)

    # Quit the FTP connection
    ftp.quit()

    print(f"{file_name} downloaded successfully!")

# Call the function
download_file()

Following is the output of the above code −

example.txt downloaded successfully!

Uploading a file

As same as downloading a file here, we assign the file name to a variable, then store the binary data to the filename, with data from the file name locally.

def upload_file():

file_name = 'exampleFile.txt'
ftp.storbinary('STOR ' + file_name, open(file_name, 'rb'))
ftp.quit()
  
upload_file()

Printing the File List

The below example code will connect to an FTP server and print the file list from the home directory of that server.

import ftplib

# Connect to the FTP server
ftp_connection = ftplib.FTP('ftp.server.com', 'username', '@email.address')

# Printing file list
print("File List:")
file_list = ftp_connection.dir()
print(file_list)

# Change working directory to /tmp
ftp_connection.cwd("/tmp")

Following is the output of the above code −

File List:
-rw-r--r--   1 owner    group        12345 Sep 11 12:00 file1.txt
-rw-r--r--   1 owner    group        67890 Sep 10 15:30 file2.zip
Updated on: 2024-11-13T12:47:27+05:30

387 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements