The ftplib module contains the implementation of the File Transfer Protocol (FTP) client.
The following example shows how to log in and get a list of incoming directories. The dir function is passed a callback function that is called once per line when the server responds. The default callback function of the ftplib module simply hits the corresponding sys.stdout .
It is worth noting that the format of the directory listing is server-dependent (usually, the format of the directory obtained is the same as the format of the server platform).
Example: Get a list of folders using the ftplib module
import ftplib
ftp = ftplib.FTP("www.python.org")
ftp.login("anonymous", "ftplib-example-1")
data = []
ftp.dir(data.append)
ftp.quit()
for line in data:
print "-", line
The result is as follows:
$ python ftplib-example-1.py
- total 34
- drwxrwxr-x 11 root 4127 512 Sep 14 14:18 .
- drwxrwxr-x 11 root 4127 512 Sep 14 14:18 ..
- drwxrwxr-x 2 root 4127 512 Sep 13 15:18 RCS
- lrwxrwxrwx 1 root bin 11 Jun 29 14:34 README -> welcome.msg
- drwxr-xr-x 3 root wheel 512 May 19 1998 bin
- drwxr-sr-x 3 root 1400 512 Jun 9 1997 dev
- drwxrwxr-- 2 root 4127 512 Feb 8 1998 dup
- drwxr-xr-x 3 root wheel 512 May 19 1998 etc
...
File download is relatively simple, use the retr function appropriately, and pay attention when downloading text files, you need to add the end of the file yourself. The following function uses a lambda expression to quickly add an end of file.
Example: Retrieving files using the ftplib module
import ftplib
import sys
def gettext(ftp, filename, outfile=None):
# fetch a text file
if outfile is None:
outfile = sys.stdout
# use a lambda to add newlines to the lines read from the server
ftp.retrlines("RETR " + filename, lambda s, w=outfile.write: w(s+"\n"))
def getbinary(ftp, filename, outfile=None):
# fetch a binary file
if outfile is None:
outfile = sys.stdout
ftp.retrbinary("RETR " + filename, outfile.write)
ftp = ftplib.FTP("www.python.org")
ftp.login("anonymous", "ftplib-example-2")
gettext(ftp, "README")
getbinary(ftp, "welcome.msg")
The result is as follows:
$ python ftplib-example-2.py
WELCOME to python.org, the Python programming language home site.
You are number %N of %M allowed users. Ni!
Python Web site: http://www.python.org/
CONFUSED FTP CLIENT? Try begining your login password with '-' dash.
This turns off continuation messages that may be confusing your client.
...
Finally, the following example copies files to an FTP server. This script uses file extensions to identify whether a file is text or binary.
Example: Storing files using the ftplib module
import ftplib
import os
def upload(ftp, file):
ext = os.path.splitext(file)[1]
if ext in (".txt", ".htm", ".html"):
ftp.storlines("STOR " + file, open(file))
else:
ftp.storbinary("STOR " + file, open(file, "rb"), 1024)
ftp = ftplib.FTP("ftp.fbi.gov")
ftp.login("mulder", "trustno1")
upload(ftp, "trixie.zip")
upload(ftp, "file.txt")
upload(ftp, "sightings.jpg")
Reprint please indicate the source: A cat learns to program
More tutorials: A cat learns to program-python basic tutorial