ftplib
The Python ftplib module implements the client side of the File Transfer Protocol (FTP), allowing Python programs to connect to FTP servers and transfer files programmatically.
Here’s a quick look at listing a directory on an FTP server:
from ftplib import FTP
with FTP("ftp.example.com") as ftp:
ftp.login()
ftp.cwd("pub")
print(ftp.nlst())
# ['README', 'data.tar.gz', 'changelog.txt']
The module provides two classes: FTP for plain connections and FTP_TLS for connections secured with TLS.
Note: The ftp.example.com server is a placeholder and won’t respond if you run the code snippets as they are. Replace the hostname and credentials with your own to try the examples against a real server.
Key Features
- Implements RFC 959 with UTF-8 encoding support per RFC 2640
- Supports both binary and line-mode file transfers
- Uses passive mode by default, which works through most firewalls
- Provides
FTP_TLSfor TLS-encrypted control connections and opt-in data channel encryption viaprot_p() - Supports context manager usage for automatic connection cleanup
- Covers directory listing, navigation, upload, download, rename, and delete operations
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
ftplib.FTP |
Class | Opens a plain FTP connection to a server |
ftplib.FTP_TLS |
Class | Opens an FTP connection secured with TLS |
FTP.login() |
Method | Authenticates with the server (defaults to anonymous login) |
FTP.retrbinary() |
Method | Downloads a file in binary mode by passing each block to a callback |
FTP.storbinary() |
Method | Uploads a file in binary mode from a file-like object |
FTP.retrlines() |
Method | Downloads a file or directory listing line by line |
FTP.mlsd() |
Method | Lists a directory in standardized format, returning filename and metadata pairs |
FTP.cwd() |
Method | Changes the working directory on the server |
FTP.pwd() |
Method | Returns the current working directory path on the server |
FTP.delete() |
Method | Deletes a file from the server |
FTP.rename() |
Method | Renames or moves a file on the server |
ftplib.all_errors |
Tuple | Contains the four ftplib exception types plus OSError and EOFError, for broad error handling |
Examples
Downloading a file in binary mode:
from ftplib import FTP
with FTP("ftp.example.com") as ftp:
ftp.login()
with open("data.tar.gz", "wb") as f:
ftp.retrbinary("RETR data.tar.gz", f.write)
Uploading a file in binary mode:
from ftplib import FTP
with FTP("ftp.example.com") as ftp:
ftp.login("alice", "secret")
with open("report.pdf", "rb") as f:
ftp.storbinary("STOR report.pdf", f)
Connecting securely with FTP_TLS and protecting the data channel:
from ftplib import FTP_TLS
with FTP_TLS("ftp.example.com") as ftps:
ftps.login("alice", "secret")
ftps.prot_p() # Secure the data connection
ftps.cwd("uploads")
with open("backup.tar.gz", "rb") as f:
ftps.storbinary("STOR backup.tar.gz", f)
Common Use Cases
The most common tasks for ftplib include:
- Automating scheduled file uploads to legacy FTP servers
- Downloading archives or data feeds from public FTP mirrors
- Mirroring remote FTP directories to a local filesystem
- Connecting to secure FTP servers using
FTP_TLSwith anssl.SSLContext
Real-World Example
The following script downloads all .csv files from a directory on an FTP server:
ftp_download_csv.py
from ftplib import FTP, all_errors
from pathlib import Path
OUTPUT_DIR = Path("downloads")
OUTPUT_DIR.mkdir(exist_ok=True)
try:
with FTP("ftp.example.com") as ftp:
ftp.login()
ftp.cwd("data/reports")
csv_files = [name for name in ftp.nlst() if name.endswith(".csv")]
for filename in csv_files:
dest = OUTPUT_DIR / filename
with open(dest, "wb") as f:
ftp.retrbinary(f"RETR {filename}", f.write)
print(f"Downloaded: {filename}")
except all_errors as e:
print(f"FTP error: {e}")
Run it:
$ python ftp_download_csv.py
Downloaded: sales_jan.csv
Downloaded: sales_feb.csv
Downloaded: summary.csv
The script uses nlst() to filter filenames by extension and retrbinary() to download each file into a local downloads/ directory. Wrapping the session in all_errors catches any connection, authentication, or protocol failure in one place.
Related Resources
Tutorial
How to Download Files From URLs With Python
In this tutorial, you'll find the right tools to help you download files from URLs with Python and manage the data retrieval process. You'll cover data streaming, thread pools, and asynchronous downloads.
For additional information on related topics, take a look at the following resources:
- Python's urllib.request for HTTP Requests (Tutorial)
- Sending Emails With Python (Tutorial)
- Python's Requests Library (Guide) (Tutorial)
- Socket Programming in Python (Guide) (Tutorial)
- Reading and Writing Files in Python (Guide) (Tutorial)
- Downloading Files From URLs With Python (Course)
- HTTP Requests With Python's urllib.request (Course)
- Sending Emails Using Python (Course)
- Emailing With Yagmail in Python (Course)
- Sending Emails With Python (Quiz)
- Making HTTP Requests With Python (Course)
- Python's Requests Library (Quiz)
- Programming Sockets in Python (Course)
- Socket Programming in Python (Quiz)
- Reading and Writing Files in Python (Course)
- Reading and Writing Files in Python (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Sept. 26, 2026