-
-
Save shitana/40aecd9c19cba0a387cf122cb7f861e3 to your computer and use it in GitHub Desktop.
Recursively fetch files from an FTP server directory. Here, it's downloading all the zip files found in or beneath the parent directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from ftplib import FTP | |
import os | |
class cRecursive: | |
my_dirs = [] # global | |
my_files = [] # global | |
all_dirs = [] # global | |
curdir = '' # global | |
ftp = None | |
def _getDirs(self, ln): | |
cols = ln.split(' ') | |
objname = cols[len(cols)-1] # file or directory name | |
if ln.startswith('d'): | |
self.my_dirs.append(objname) | |
else: | |
self.my_files.append(os.path.join(self.curdir, objname)) # full path | |
def _checkDirs(self, adir, f): | |
self.my_dirs = [] | |
gotdirs = [] | |
self.curdir = ftp.pwd() | |
print "going to change to directory " + adir + " from " + self.curdir | |
f.cwd(adir) | |
self.curdir = f.pwd() | |
print "now in directory: " + self.curdir | |
f.retrlines('LIST', self._getDirs) | |
gotdirs = self.my_dirs | |
self.all_dirs.append(self.curdir) | |
for subdir in gotdirs: | |
self.my_dirs = [] | |
self._checkDirs(subdir, ftp) # recurse | |
f.cwd('..') # back up a directory when done here | |
def _listFiles(self, origin, f): | |
self._checkDirs(origin, f) # directory to start in | |
f.cwd(origin) # change to root directory for idownloading | |
def _createDirs(self, dest): | |
### Create Dirs ### | |
for dirname in self.all_dirs: | |
localdir = "%s/%s" % (dest, dirname) | |
print 'create dir: ' + localdir | |
if not os.path.isdir(localdir): | |
os.makedirs(localdir) | |
################### | |
def _createFiles0(self, ftp, dest): | |
for filename in self.my_files: | |
localfile = "%s%s" % (dest, filename) | |
fileObj = open(localfile, 'wb') | |
print 'getting ' + filename + ' in ' + dest | |
f.retrbinary('RETR ' + filename, fileObj.write) | |
def _createFiles(self, f, dest): | |
for filename in self.my_files: | |
try: | |
print ("\t* Copying file %s into dir %s" % (filename, dest)) | |
localfile = "%s/%s" % (dest, filename) | |
print("Copy to {0}".format(localfile)) | |
fileObj = open(localfile, 'wb') | |
f.retrbinary('RETR ' + filename, fileObj.write) | |
fileObj.close() | |
except Exception as e: | |
print("Unable to get file %s : %s" % (filename, e)) | |
try: | |
destination = "<YOUR_DESTINATION_DIR>" | |
origin = "<FTP_ROOT_PATH>" | |
ftp = FTP('FTP_IP') | |
ftp.login("FTP_USER", "FTP_PASSWORD") | |
ftpRecursive = cRecursive() | |
ftpRecursive._checkDirs(origin, ftp) # directory to start in | |
ftpRecursive._createDirs(destination) | |
ftpRecursive._createFiles(ftp, destination) | |
ftp.cwd(origin) # change to root directory for idownloading | |
ftp.quit() | |
except Exception as error: | |
print 'Error: %s' % error | |
ftp.quit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment