Downloading a directory tree with ftplib

前端 未结 6 1685
名媛妹妹
名媛妹妹 2020-11-27 06:25

This will not download the contents of sub-directories; how can I do so?

import ftplib
import configparser
import os

directories = []

def add_directory(lin         


        
6条回答
  •  广开言路
    2020-11-27 07:00

    This is a very old question, but I had a similar need that i wanted to satisfy in a very general manner. I ended up writing my own solution that works very well for me. I've placed it on Gist here https://gist.github.com/Jwely/ad8eb800bacef9e34dd775f9b3aad987

    and pasted it below in case i ever take the gist offline.

    Example usage:

    import ftplib
    ftp = ftplib.FTP(mysite, username, password)
    download_ftp_tree(ftp, remote_dir, local_dir)
    

    The code above will look for a directory called "remote_dir" on the ftp host, and then duplicate the directory and its entire contents into the "local_dir". It invokes the script below.

    import ftplib
    import os
    
    def _is_ftp_dir(ftp_handle, name, guess_by_extension=True):
        """ simply determines if an item listed on the ftp server is a valid directory or not """
    
        # if the name has a "." in the fourth to last position, its probably a file extension
        # this is MUCH faster than trying to set every file to a working directory, and will work 99% of time.
        if guess_by_extension is True:
            if name[-4] == '.':
                return False
    
        original_cwd = ftp_handle.pwd()     # remember the current working directory
        try:
            ftp_handle.cwd(name)            # try to set directory to new name
            ftp_handle.cwd(original_cwd)    # set it back to what it was
            return True
        except:
            return False
    
    
    def _make_parent_dir(fpath):
        """ ensures the parent directory of a filepath exists """
        dirname = os.path.dirname(fpath)
        while not os.path.exists(dirname):
            try:
                os.mkdir(dirname)
                print("created {0}".format(dirname))
            except:
                _make_parent_dir(dirname)
    
    
    def _download_ftp_file(ftp_handle, name, dest, overwrite):
        """ downloads a single file from an ftp server """
        _make_parent_dir(dest)
        if not os.path.exists(dest) or overwrite is True:
            with open(dest, 'wb') as f:
                ftp_handle.retrbinary("RETR {0}".format(name), f.write)
            print("downloaded: {0}".format(dest))
        else:
            print("already exists: {0}".format(dest))
    
    
    def _mirror_ftp_dir(ftp_handle, name, overwrite, guess_by_extension):
        """ replicates a directory on an ftp server recursively """
        for item in ftp_handle.nlst(name):
            if _is_ftp_dir(ftp_handle, item):
                _mirror_ftp_dir(ftp_handle, item, overwrite, guess_by_extension)
            else:
                _download_ftp_file(ftp_handle, item, item, overwrite)
    
    
    def download_ftp_tree(ftp_handle, path, destination, overwrite=False, guess_by_extension=True):
        """
        Downloads an entire directory tree from an ftp server to the local destination
    
        :param ftp_handle: an authenticated ftplib.FTP instance
        :param path: the folder on the ftp server to download
        :param destination: the local directory to store the copied folder
        :param overwrite: set to True to force re-download of all files, even if they appear to exist already
        :param guess_by_extension: It takes a while to explicitly check if every item is a directory or a file.
            if this flag is set to True, it will assume any file ending with a three character extension ".???" is
            a file and not a directory. Set to False if some folders may have a "." in their names -4th position.
        """
        os.chdir(destination)
        _mirror_ftp_dir(ftp_handle, path, overwrite, guess_by_extension)
    

提交回复
热议问题