Downloading a Directory Tree with FTPLIB
- by Anthony Lemmer
I'd like to download a directory and all of its contents to the local HD.
Here's the code I have thus far (crashes if there's a sub-directory, else grabs all the files):
import ftplib
import configparser
import os
def runBackups():
#Load INI
filename = 'connections.ini'
config = configparser.SafeConfigParser()
config.read(filename)
connections = config.sections()
i = 0
while i < len(connections):
#Load Settings
uri = config.get(connections[i], "uri")
username = config.get(connections[i], "username")
password = config.get(connections[i], "password")
backupPath = config.get(connections[i], "backuppath")
archiveTo = config.get(connections[i], "archiveto")
#Start Back-ups
ftp = ftplib.FTP(uri)
ftp.login(username, password)
ftp.set_debuglevel(2)
ftp.cwd(backupPath)
files = ftp.nlst()
for filename in files:
ftp.retrbinary('RETR %s' % filename, open(os.path.join(archiveTo, filename), 'wb').write)
ftp.quit()
i += 1
print()
print("Back-ups complete.")
print()