There are lots of different ways to download a file from the internet using Python. One popular way is to connect to an FTP server and download your files that way. So that is what we will be looking at in this article. All you need is your standard installation of Python. It includes a library called ftplib, which has all the bits and pieces we need to accomplish this task.
Downloading the file is actually quite easy. Following is one simple example for how to do it:
# ftp-ex.py import os from ftplib import FTP ftp = FTP("www.myWebsite.com", "USERNAME", "PASSWORD") ftp.login() ftp.retrlines("LIST") ftp.cwd("folderOne") ftp.cwd("subFolder") # or ftp.cwd("folderOne/subFolder") listing =  ftp.retrlines("LIST", listing.append) words = listing.split(None, 8) filename = words[-1].lstrip() # download the file local_filename = os.path.join(r"c:\myfolder", filename) lf = open(local_filename, "wb") ftp.retrbinary("RETR " + filename, lf.write, 8*1024) lf.close()
Let’s break this down a bit. First off, we need to login to the FTP server, so you’ll pass in the URL along with your credentials or you can skip that if it’s one of those anonymous FTP servers. The retrlines(“LIST”) command will give us a directory listing. The cwd command stands for “change working directory”, so if the current directory doesn’t have what you’re looking for, you’ll need to use cwd to change to the one that does. The next section shows how to grab the file name in a rather stupid way. You could also use os.path.basename to get the same thing in most cases. The last section shows how to actually download the file. Note that we have to open the file handler with the “wb” (write binary) flag so we get the file downloaded correctly. The “8*1024″ bit is the blocksize to download, although Python is pretty smart about choosing a reasonable default.
Note: This article is based on the Python documentation for the ftplib module and the following script found in your default Python’s install folder: Tools/scripts/ftpmirror.py.