如何解决从ftp服务器下载带有文件的子目录
我创建了一个函数,可以从给定的ftp服务器下载.Z文件,并即时取消压缩它们,但是在这种情况下,我要访问的目录具有子目录。我想下载所有这些子目录和其中的文件,我也希望文件也被解压缩。我应该如何修改此功能?我对此并不陌生,我知道我可以实现递归,但现在还不知道如何实现。请帮忙,因为我为此苦了一个星期。
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('.Z'):
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 举报,一经查实,本站将立刻删除。