Downloading a Directory Tree with FTPLIB

Posted by Anthony Lemmer on Stack Overflow See other posts from Stack Overflow or by Anthony Lemmer
Published on 2010-04-09T04:40:26Z Indexed on 2010/04/09 4:43 UTC
Read the original article Hit count: 395

Filed under:
|
|

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()

© Stack Overflow or respective owner

Related posts about python

Related posts about ftp