def get_contents(path, host, username, password): """ Will return a list of the contents in a given path on a FTP-server. """ try: logger.info('Connecting to ftp://%s:%s@%s' % (username, password, host)) ftp = FTP(host, username, password) except Exception, exception: logger.error('Error connecting to %s: %s' % (host, exception))
def get_filelist(files, path, host, username, password, localpath=''): """ Will retrieve a list of files from a specific path on a given host, and store it locally at some (optional) path """ try: logger.info('Connecting to ftp://%s:%s@%s' % (username, password, host)) ftp = FTP(host, username, password) except Exception, exception: logger.error('Error connecting to %s: %s' % (host, exception))
def get_file(filename, path, host, username, password, localpath=''): """ Will retrieve a specified file from a specific path on a given host, and store it a the local path. """ try: logger.info('Connecting to ftp://%s:%s@%s' % (username, password, host)) ftp = FTP(host, username, password) except Exception, exception: logger.error('Error connecting to %s: %s' % (host, exception))
def insert_path(path): """ Adding all files in the folder """ logger.info('Adding everything in %s.' % path) chdir(path) try: for item in listdir(path): insert(item) except OSError, exception: logger.error('Could not read files in path: %s' % exception)
def clean(): """ Erasing all temporary files """ logger.info('Cleaning the temporary directory.') chdir(config.tempdir) try: for item in listdir(config.tempdir): remove(item) except OSError, exception: logger.error('Could not delete files: %s' % exception)
chdir(localpath) else: if not exists(config.tempdir): mkdir(config.tempdir) chdir(config.tempdir) for filename in files: with open(filename, 'w') as outfile: ftp.retrbinary('RETR %s' % filename, outfile.write) outfile.close() ftp.quit() def get_contents(path, host, username, password): """ Will return a list of the contents in a given path on a FTP-server. """ try: logger.info('Connecting to ftp://%s:%s@%s' % (username, password, host)) ftp = FTP(host, username, password) except Exception, exception: logger.error('Error connecting to %s: %s' % (host, exception)) logger.info('Changing path to %s on FTP server.' % path) ftp.cwd(path) files = [] try: files = ftp.nlst() except Exception, exception: logger.error('Error retrieving file list: %s' % exception) for item in files: if not 'xml' in item: files.remove(item) return files