Movatterモバイル変換


[0]ホーム

URL:


How to Download and Upload Files in FTP Server using Python

Learn how to use Python's built-in ftplib module to download and upload files in a FTP server using RETR and STOR commands respectively.
  · 3 min read · Updated may 2024 ·Python Standard Library

Want to code faster? OurPython Code Generator lets you create Python scripts with just a few clicks. Try it now!

One of the main features of an FTP server is the ability to store and retrieve files. In this tutorial, you will learn how you can download and upload files on an FTP server using Python.

We will be using Python's built-inftplib module; we gonna use a test FTP server for this tutorial; it is calledDLPTEST. Let's define its information:

import ftplibFTP_HOST = "ftp.dlptest.com"FTP_USER = "dlpuser@dlptest.com"FTP_PASS = "SzMf7rTE4pCrf9dV286GuNe4N"

The password can change from time to time. Make sure you visittheir website for the correct credentials, connecting to this server:

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

Uploading Files

To upload a file, we gonna need to use theftp.storbinary() method; the below code handles that:

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

We opened the file with the"rb" mode, which means we read the local file in binary mode.

After that, we used the FTP'sSTOR command, which stores the file in binary mode, it transfers that file to a new port. Note that the file must exist in your local working directory; otherwise, this won't work.

This test server will delete the file after30 minutes. To make sure the file is successfully uploaded, we need tolist all files and directories using the ftp.dir() method:

# list current files & directoriesftp.dir()

Sure enough, the file is there:

drwxr-xr-x    2 dlptest9   dlptest9        40960 Apr 11 07:04 .drwxr-xr-x    2 dlptest9   dlptest9        40960 Apr 11 07:04 ..-rw-r--r--    1 dlptest9   dlptest9          172 Apr 11 07:00 357299070163503-2020-04-11-11-59.txt-rw-r--r--    1 dlptest9   dlptest9          171 Apr 11 07:01 357299070163503-2020-04-11-12-00.txt-rw-r--r--    1 dlptest9   dlptest9          171 Apr 11 07:02 357299070163503-2020-04-11-12-01.txt-rw-r--r--    1 dlptest9   dlptest9          171 Apr 11 07:03 357299070163503-2020-04-11-12-02.txt-rw-r--r--    1 dlptest9   dlptest9           20 Apr 11 07:04 some_file.txt-rw-r--r--    1 dlptest9   dlptest9           24 Apr 11 07:00 syslogtest_be.txt

Downloading Files

Now let's try to download that same file again:

# the name of file you want to download from the FTP serverfilename = "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)

This time, we're opening the local file in"wb" mode, as we will write the file from the server to the local machine.

We're usingRETR command, which downloads a copy of a file on the server, we provide the file name we want to download as the first argument to the command, and the server will send a copy of the file to us.

Theftp.retrbinary() method takes the method to call when storing the file on the local machine as a second argument.

If you have deleted that file and run the above code, you'll see the file will appear again; we've successfully downloaded the file!

Finally, you have to quit and close the FTP connection:

# quit and close the connectionftp.quit()

Alright, we are done with this quick tutorial. I have separated the code for downloading and uploading scripts; check themhere.

Related: How to List all Files and Directories in FTP Server using Python.

Happy Learning ♥

Save time and energy with ourPython Code Generator. Why start from scratch when you can generate? Give it a try!

View Full Code Improve My Code
Sharing is caring!



Read Also


How to List all Files and Directories in FTP Server using Python
How to Brute Force FTP Servers in Python
How to Download Files from URL in Python

Comment panel

    Got a coding query or need some guidance before you comment? Check out thisPython Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!





    Ethical Hacking with Python EBook - Topic - Top


    Join 50,000+ Python Programmers & Enthusiasts like you!



    Tags


    New Tutorials

    Popular Tutorials


    Ethical Hacking with Python EBook - Topic - Bottom

    CodingFleet - Topic - Bottom






    Claim your Free Chapter!

    Download a Completely Free Practical Python PDF Processing Chapter.

    See how the book can help you build handy PDF Processing tools!



    [8]ページ先頭

    ©2009-2025 Movatter.jp