def walk(top): """Directory tree generator. For each directory in the directory tree rooted at top (including top itself, but excluding '.' and '..'), yields a 3-tuple dirpath, dirnames, filenames dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists are just names, with no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name). """ (hostname,path,user,password) = parseuri(top) host = FTPHost(hostname,user,password) for dir in host.walk(path): yield dir
def recursively_download_ftp(host, username, password, ftp_root_path, local_dest_dir): # Data preparation ftp_root_path = sub('\\\\', '/', ftp_root_path) ftp_root_path = sub('^[/]*', '/', ftp_root_path) ftp_root_path = sub('/$', '', ftp_root_path) print('Recursively downloading from ftp://{0}:{1}@{2}{3} to {4}'.format( username, password, host, ftp_root_path, local_dest_dir)) host = FTPHost(host, username, password) recursive_file_walk = host.walk(ftp_root_path, topdown=True, onerror=None) for folder_path, _, folder_files in recursive_file_walk: print('REMOTE DIR\t', folder_path) short_folder_path = sub('^' + ftp_root_path, '', folder_path) local_folder_path = local_dest_dir if short_folder_path: local_folder_path = path.join(local_dest_dir, sub('^/', '', short_folder_path)) if not path.exists(local_folder_path): makedirs(local_folder_path) print('LOCAL DIR\t', local_folder_path) if len(folder_files) > 0: for folder_file in folder_files: remote_path = folder_path + '/' + folder_file print('REMOTE FILE\t', remote_path) local_file_path = path.join(local_folder_path, folder_file) local_file_path = path.abspath(local_file_path) print('LOCAL FILE\t', local_file_path) host.download_if_newer(remote_path, local_file_path) else: print('NO FILES') print('') host.close