On this page
  1. ftplib
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Uploading Files and Listing Structured Metadata
  7. Gotchas & Boundaries
  8. Classes & Objects
  9. Constants & Attributes

ftplib

Prev: fractions | Up: stdlib_module | Next: functools
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org — ftplib
Tags: #public #python3 #stdlib


Import

import ftplib

Overview

FTP and explicit FTPS client (FTP, FTP_TLS) for listing remote directories, uploading (storbinary), and downloading (retrbinary) files. Use ftplib when automating transfers with legacy FTP/FTPS endpoints; for SFTP (SSH File Transfer Protocol), use paramiko or asyncssh instead.

Quick Start

import ftplib
import io

def download_bytes_ftps(host: str, user: str, credential: str, remote_path: str) -> bytes:
    buf = io.BytesIO()
    with ftplib.FTP_TLS(host, timeout=30) as ftp:
        ftp.login(user, credential)
        ftp.prot_p()   # encrypt the data connection, not just control/login
        ftp.retrbinary(f"RETR {remote_path}", buf.write)
    return buf.getvalue()

Implementation Walkthrough

1. Uploading Files and Listing Structured Metadata

Use storbinary() with a binary file object for uploads, and prefer mlsd() over nlst()/dir() when the server supports RFC 3659 machine-readable listings:

import ftplib
from pathlib import Path

def upload_file(ftp: ftplib.FTP_TLS, local_path: Path, remote_name: str) -> None:
    with local_path.open("rb") as fh:
        ftp.storbinary(f"STOR {remote_name}", fh)

Gotchas & Boundaries

Classes & Objects

Constants & Attributes

Linked from

Related notes