def copy_dir(ftp, filename, outputdir, prefix, pattern='<STR_LIT>'):
try:<EOL><INDENT>mode = ftp.stat(filename).st_mode<EOL><DEDENT>except IOError as e:<EOL><INDENT>if e.errno == errno.ENOENT:<EOL><INDENT>logger.error("<STR_LIT>" + filename + "<STR_LIT>")<EOL>pass<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if mode & stat.S_IFREG:<EOL><INDENT>if re.match(pattern, filename) is not None:<EOL><INDENT>new_file = os.path.join(outputdir, "<STR_LIT>".format(prefix, os.path.basename(filename)))<EOL>ftp.get(filename, new_file)<EOL><DEDENT><DEDENT>elif mode & stat.S_IFDIR:<EOL><INDENT>for f in ftp.listdir(filename):<EOL><INDENT>copy_dir(ftp, os.path.join(filename, f), outputdir,<EOL>"<STR_LIT>".format(prefix, os.path.basename(filename)), pattern)<EOL><DEDENT><DEDENT><DEDENT>
Recursively copy a directory flattens the output into a single directory but
prefixes the files with the path from the original input directory
:param ftp:
:param filename:
:param outputdir:
:param prefix:
:param pattern: a regex pattern for files to match (by default matches everything)
:return: