从ftp服务器下载包含文件的文件夹

时间:2020-08-20 15:14:17

标签: python gzip ftplib

我创建了一个函数,可以从给定的ftp服务器下载.gz文件,并即时取消压缩它们,但是在这种情况下,我要访问的目录具有子目录。我想下载所有这些子目录以及其中的所有文件,我也希望文件也被解压缩。我应该如何修改此功能?

ionex_domain = "ftp://cddis.gsfc.nasa.gov/gnss/products/troposphere/zpd/" #domain with subdirectories
year = '2013'

    def download(ionex_domain, year):
        user = getpass.getuser()
        ionex_parse = urlparse(ionex_domain) 
    
        ionex_connection = FTP(ionex_parse.netloc)
        ionex_connection.login()
        ionex_connection.cwd(ionex_parse.path + year)
        ionex_files = ionex_connection.nlst()
    
        ionex_userpath = "C:\\Users\\" + user + "\\DCBviz\\ionex\\" + year
        pathlib.Path(ionex_userpath).mkdir(parents=True, exist_ok=True) 
        
        if os.path.exists(ionex_userpath):
            shutil.rmtree(ionex_userpath)
            os.makedirs(ionex_userpath)
    
        for fileName in ionex_files:
            local_filename = os.path.join(ionex_userpath, fileName)
            if local_filename.endswith('.gz'):
                local_filename = local_filename[:-3]
            data = BytesIO()
            ionex_connection.retrbinary('RETR '+ fileName, data.write, 1024)
            data.seek(0)
            uncompressed = gzip.decompress(data.read())
            with open(local_filename, 'wb') as file:
                file.write(uncompressed)
    download(ionex_domain, year)

0 个答案:

没有答案