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

问题描述

我创建了一个函数,可以从给定的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)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)