Python ftp get files

Как использовать Python для работы с FTP

Узнайте, как использовать Python для работы с FTP-серверами: подключение, загрузка/скачивание файлов и управление ими!

Transferring files to an FTP server using Python.

В данной статье мы рассмотрим, как использовать Python для работы с FTP (File Transfer Protocol) — протоколом передачи файлов. FTP — это стандартный сетевой протокол, который используется для передачи файлов между клиентом и сервером через интернет или локальную сеть.

Подключение к FTP-серверу с использованием Python

Для работы с FTP в Python используется библиотека ftplib . Если у вас её ещё нет, установите её с помощью команды:

Теперь давайте подключимся к FTP-серверу:

from ftplib import FTP ftp = FTP('ftp.example.com') ftp.login(user='username', passwd='password')

Здесь мы импортируем класс FTP из библиотеки ftplib и создаем экземпляр ftp , подключаясь к серверу ftp.example.com . Затем мы вызываем метод login для аутентификации с использованием имени пользователя и пароля.

Загрузка и скачивание файлов с FTP-сервера

Чтобы загрузить файл на FTP-сервер, используйте метод storbinary :

with open('local_file.txt', 'rb') as file: ftp.storbinary('STOR remote_file.txt', file)

Здесь мы открываем файл local_file.txt на нашем компьютере в режиме чтения бинарных данных и загружаем его на сервер под именем remote_file.txt .

Для скачивания файла с FTP-сервера используйте метод retrbinary :

with open('downloaded_file.txt', 'wb') as file: ftp.retrbinary('RETR remote_file.txt', file.write)

Здесь мы открываем файл downloaded_file.txt на нашем компьютере в режиме записи бинарных данных и скачиваем файл remote_file.txt с сервера.

Просмотр списка файлов на FTP-сервере

Чтобы получить список файлов на FTP-сервере, используйте метод nlst :

files = ftp.nlst() print(files)

Этот метод возвращает список строк с именами файлов и директорий на сервере.

Читайте также:  Sending email with php and gmail

Закрытие соединения с FTP-сервером

Не забудьте закрыть соединение с сервером после завершения работы с ним:

Метод quit закрывает соединение с FTP-сервером и завершает сеанс.

Таким образом, мы рассмотрели основные операции, которые можно выполнять с использованием Python и библиотеки ftplib для работы с FTP-серверами. Надеемся, что эта информация была полезной для вас! 🚀

Источник

Work with FTP Server using Python: Complete Guide

In this tutorial we will explore how to work with FTP server using Python.

Table of Contents

Introduction

FTP (File Transfer Protocol) is a set of rules describing how the files are transferred within a computer network.

Many companies using FTP to securely send large files across the organization.

For programmers it is often a requirement to send output files such as model predictions or test results using FTP so the files can be retrieved by the stakeholders across different business units.

In this tutorial we will use a public FTP test server DLP Test that offers free FTP server for testing purposes and automatically deletes the files after a few minutes.

Connect to FTP server using Python

The first step to start working with FTP server using Python is to connect to the FTP server.

Let’s import the required dependency:

In order to connect to the FTP server, you will need to know some credentials:

Let’s define the credentials as Python variables:

And finally establish a connection to the FTP server using Python:

Now the connection is established and we can verify it by requesting a welcome message from the FTP server:

'220 Welcome to the DLP Test FTP Server'

Note : not all FTP servers have welcome messages, in case of the DLP Test FTP server, a welcome message should be displayed.

List files in the FTP server using Python

One of the simplest operations with FTP server is to retrieve a list of files in the directory.

Currently we are in the root directory and it can be verified by running:

Now we can get a list of files in the current directory. There are two ways of doing it:

Читайте также:  Java security system property

    Produce a directory listing which prints it to standard output

Produce a directory listing

which should print a standard output with something like:

-rw-r--r-- 1 1001 1001 958 Aug 15 17:30 file1.txt -rw-r--r-- 1 1001 1001 958 Aug 15 17:30 file2.txt -rw-r--r-- 1 1001 1001 0 Aug 15 17:31 file3.txt

Return a list of file names in a directory

which should return a Python list like:

['file1.txt', 'file2.txt', 'file3.txt']

Note : the filenames returned by both commands will be identical.

Create a new directory in the FTP server using Python

Before we start testing uploading files to FTP, let’s create our own folder for these test cases.

Since this FTP server is used by many developers to test their FTP code, we will create a subdirectory so it’s easier to follow the tutorial.

Our folder will be called ‘pyshark’.

and the folder will be created.

You can verify it either by connecting to FTP server using FileZilla or my printing a list of file names in the directory, and one of them will be ‘pyshark’, which is the folder that we have just created.

Next, lets set this new directory as the current directory on the server by running:

and to validate the change, print the name of the current directory:

Sample file

In order to continue in this tutorial we will need some sample file to work with.

We will create a sample TXT file with test: “This is a sample file.”

Upload files to FTP server

In this section we will explore how to upload a file to the FTP server.

So far we have create a new folder in FTP called “pyshark” and set it as the current working directory.

In order to upload the file to FTP server, we will use the .storbinary() method of the FTP class. This method initiates a file transfer from FTP client to FTP server using the FTP command STOR.

First we will open the sample file “file1.txt” as a binary file with read mode and then perform the FTP file transfer (we will name the transferred file on the server as “uploaded_file”):

Читайте также:  Блочные элементы

To check if it was uploaded simply produce a list of files in the directory:

Download files from FTP server

In this section we will explore how to download a file from the FTP server.

We continue to work with “pyshark” folder as the current working directory.

In order to download a file from FTP server, we will use the .retrbinary() method of the FTP class. This method retrieves a file from the FTP server to the local system in binary mode using the FTP command RETR.

We are going to download the file that we uploaded to the FTP server in the previous section.

First we will create a file object “downloaded_file.txt” in as a binary file with write mode and then download the “uploaded_file.txt” from the FTP server:

The “downloaded_file.txt” should appear in the same directory as the file with the code:

Rename files in FTP server

In this section we will explore how to rename a file in the FTP server.

We continue to work with “pyshark” folder as the current working directory.

In order to delete a file from FTP server, we will use the .delete() method of the FTP class. This method takes two parameter which are the original file name and the new file name.

Let’s delete the “uploaded_file.txt” to “renamed_file.txt” in the FTP server:

To check if it was uploaded simply produce a list of files in the directory:

Delete files from FTP server

In this section we will explore how to delete a file from the FTP server.

We continue to work with “pyshark” folder as the current working directory.

In order to delete a file from FTP server, we will use the .delete() method of the FTP class. This method takes one parameter which is the file name and then deletes it from the directory.

Let’s delete the “uploaded_file.txt” from the FTP server:

To check if it was uploaded simply produce a list of files in the directory:

Источник

Оцените статью