• Hire Developers
    Hire Python Developers
    hire dedicated python developers

    Hire Python Developers

    Hire Python developers from Citrusbug to empower your business with top-notch Python development solutions. Choose from a wide range of Python development.

    Hire Django Developers
    hire dedicated django developer

    Hire Django Developers

    Leverage Citrusbug's top-notch Django development services that are designed to build mobile and web apps that perform, scale, and grow.

    Hire ReactJS Developers
    hire dedicated reactjs developer

    Hire ReactJS Developers

    Citrusbug is a one-stop destination where your search for experienced, skilled, and dedicated ReactJS developers ends.

    Hire React Native Developers
    hire react native developer india

    Hire React Native Developers

    Hire React Native developers at an economical price from our large pool of skilled professionals that leverage the ideal roadmap to business application success.

    Hire AngularJS Developers
    hire dedicated angularjs developer india

    Hire AngularJS Developers

    Hire AngularJS developers from Citrusbug to build secure, scalable, high-performance, and enterprise-grade AngularJS web applications.

    Hire VueJS Developers
    hire dedicated vuejs developers india

    Hire VueJS Developers

    Hire Vue.js developers from a leading Vue.js development company to build high-performing solutions. Our developers possess vast experience.

    Hire PHP Developers
    hire dedicated php developers india

    Hire PHP Developers

    Hire PHP developers from Citrusbug to earn a competitive edge by building robust, scalable, and complex eCommerce solutions, enterprise-grade websites.

    Hire Front End Developers
    hire front end developers india

    Hire Front End Developers

    Hire front end developers from Citrusbug for quick, robust, and agile digital solutions. Our front-end development company leverages the top coders and technologies.

    Hire Back End Developers
    top hire back end developers india

    Hire Back End Developers

    Hire backend developers with a high level of proficiency and experience to create robust and resilient business applications with quick time to market.

  • Services
    SaaS Software Development
    saas software application development

    SaaS Software Development

    As a leading SaaS development company, we offer top-notch, highly advanced, high-tech, and successful SaaS-based products to our customers worldwide.

    Web Application Development
    web application development

    Web Application Development

    Citrusbug offers end-to-end web application development services by building high-performance, intuitive, secure.

    Mobile Application Development
    mobile app development company near me

    Mobile Application Development

    We design and build tailor-made mobile apps for iOS and Android platforms. Our mobile app developers have the expertise to create highly functional technologies.

    Custom Software Development
    top custom software development company

    Custom Software Development

    Citrusbug offers top-rated custom software development services for the world’s top enterprises, SMEs, and startups to build cutting-edge solutions.

    AI & ML Development
    artificial intelligence and machine learning development services

    AI & ML Development

    Automate your business operations and accelerate growth with our intelligent, smart, and result-driven AI/ML solutions.

    Cloud & DevOps
    best cloud - devops development services

    Cloud & DevOps

    Build scalable and world-class enterprise web applications that grow with your business on the cloud.

  • Work
  • Career
  • Company
    About Us
    citrusbug about us

    About Us

    More than just creating beautiful designs and unique platforms, we aspire to innovate technological solutions that transform industries.

  • Schedule Call
  • Let's Talk
attach_file

Not a robot? please drag till 50 or above and we will know you are human

Range must be higher than 50

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

Categories:

how to download and upload files in ftp using python

Quick Summary

In this blog, you will learn how to download and upload files in the FTP server using Python. The primary feature of the FTP server is the ability to store and retrieve files. Also, we will use a test FTP server called DLPTEST. Furthermore, we will use Python’s built-in module ftplib.

This blog will teach you how to download and upload files in an FTP server using python. Before moving further, let’s briefly examine what FTP is.

What is an FTP?

FTP stands for Files Transfer Protocol. FTP's main feature is transferring files from local to remote using TCP connections. FTP is also called application layer protocol. FTP has two processes for the transforming process: data connection and control connection.

As per today's topic, we will use the ftplib module of python in the FTP server for downloading and uploading files. Let’s have a brief about the ftplib module.

Describe the ftplib module.

Here, showcasing the information using a test FTP server, DLPTEST and python’s built-in ftplib module.


import ftplib

FTP_HOST = "ftp.dlptest.com"
FTP_USER = "host_username"
FTP_PASS = "password"

        

The above code tends to take credentials to the FTP_User. The password can be changed from time to time. While making changes, make sure you are visiting their website to correct it.

The following step code is to establish the connection with FTP_SERVER.


#connect to FTP Server

ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
ftp.encoding = "utf-8"
        

Uploading Files

Uploading the file using FTP Server is shown below. The local name to identify the file is “Some_file”.


filename = "some_file.txt"
with open(filename, "rb") as file:
# use FTP's STOR command to upload the file
ftp.storbinary(f"STOR {filename}", file)
        

After this code, the file starts to upload to FTP_SERVER. And to upload, we have used the STOR command. Also, “rb” opens the file as read-only in binary format and starts reading from the beginning of the file. While the binary form can be used for various purposes. As we are aware that the test server will delete the files after 30 minutes, so to make sure the files are uploaded, we will list the files and directories using the below code.


# list current files & directories
ftp.dir()
        

Output:
List current files & directories

Downloading files

Let’s download the same uploaded file.


filename = "some_file.txt"
with open(filename, "wb") as file:
# use FTP's RETR command to download the file
ftp.retrbinary(f"RETR {filename}", file.write)            
        

We will download with the command “wb”, as it will write the file from FTP_SERVER to the local machine.

Here, “wb” opens the file as write-only in binary format.

Using the RETR command, which downloads a copy of a file from the server, we can request a copy of a file by giving the command the name of the file we wish to download as the first argument.

The second argument to the ftp.retrbinary() method specifies the procedure when saving the file to the local machine.

The file can reappear even after deleting it as we rerun the code, proving the successful downloading process.

Now to close the FTP server connection, the last code will be:


#quit and close the connection
ftp.quit()
        

Codes for Uploading & Downloading files in FTP Server

Upload file in FTP Server using Python.


Ftp_file_uploader.py

import ftplib
#FTP Server Crendentials
FTP_HOST = "ftp.dlptest.com"
FTP_USER = "host_username"
FTP_PASS = "password"

#connect to FTP Server
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
ftp.encoding = "utf-8"

# local file name you want to upload
filename = "some_file.txt"
with open(filename, "rb") as file:
    # use FTP's STOR command to upload the file
    ftp.storbinary(f"STOR {filename}", file)

# list current files & directories
ftp.dir() 

# quit and close the connection
ftp.quit()
         

Download file in FTP Server using Python.


Ftp_file_downloader.py

import ftplib

FTP_HOST = "ftp.dlptest.com"
FTP_USER = "host_username"
FTP_PASS = "password"

# connect to the FTP server
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
# force UTF-8 encoding
ftp.encoding = "utf-8"

# the name of file you want to download from the FTP server

filename = "some_file.txt"
with open(filename, "wb") as file:
    # use FTP's RETR command to download the file
    ftp.retrbinary(f"RETR {filename}", file.write)

# quit and close the connection
ftp.quit()

        

Conclusion

Wrapping up here with the above-given codes to upload and download files in FTP Server using Python. To conclude, the code is running perfectly with the output shared above.

Looking to hire the Python developers

img

Hire Python Developers as per your need :

Schedule A Developer Interview And Get 15 Days Risk-Free Trial

Feel free to ask, discuss, interview, and evaluate our top-notch engineers. Verify their competencies yourself.

Guest Contribution

img

We are looking for passionate industry experts to contribute thought leadership blogs.

Our Portfolio

Our Incredible Portfolio Across Various Industry Verticals

Ready to start your dream project?

We have a TEAM to get you there.