Information wants to be free...

Recursive FTP Download with Python

This is a feature that I have missed many times, as it is not a built-in feature of the standard Python FTP library. I decided to hack together a solution myself, and it was easier than expected. Everything is downloaded in binary mode, since ASCII mode causes so many issues!

Enjoy:

#!/usr/bin/python

import ftplib
import os

def ftp_get_files(directory):
	print "Going Into:", directory
	ftp.cwd(directory)
	os.mkdir(directory)
	os.chdir(directory)

	listing = []
	ftp.dir(listing.append)
	for entry in listing: 
		filename = entry.split(None, 8)[-1]
		perm = entry.split()[0]

		if filename == '.' or filename == '..':
			continue

		if perm[0] == "d": # Directory
			ftp_get_files(filename)

		elif perm[0] == "-": # File
			print "Fetch:", filename
			ftp.retrbinary('RETR %s' % (filename), open(filename, 'wb').write)

	print "Going Back:", directory
	ftp.cwd("..")
	os.chdir("..")

if __name__ == "__main__":
	import sys

	if len(sys.argv) < 6:
		print "Usage: %s <hostname> <user> <pass> <initial dir> <download dir>" % (sys.argv[0])
		sys.exit(1)

	ftp = ftplib.FTP(sys.argv[1])
	ftp.login(sys.argv[2], sys.argv[3])
	ftp.cwd(sys.argv[4])
	ftp_get_files(sys.argv[5])
	ftp.close()

	sys.exit(0)

          


Topic: Scripts and Code, by Kjetil @ 09/03-2013, Article Link