使用 Python 在 FTP 服务器上的不同文件夹之间导航

Navigating between different folders on FTP server with Python

我想在同一个 FTP 会话中使用 Python 的 ftplib 在同一个 FTP 服务器上的两个不同文件夹之间导航。

我写的基本脚本如下(gbname和gffname赋值):

ensembl = FTP('ftp.ensemblgenomes.org')
ensembl.login()

ensembl.cwd("pub/fungi/current/genbank/")
ensembl.retrbinary('RETR ' + gbname, open(gbname, 'wb').write)

ensembl.cwd("pub/fungi/current/gff/")
ensembl.retrbinary('RETR ' + gffname, open(gffname, 'wb').write)

ensembl.quit()

此脚本在第二个 cwd 处回溯并出现以下错误 "ftplib.error_perm: 550 Failed to change directory."。

我明白为什么它回溯到那里,我可以通过启动两个不同的 FTP 会话来解决问题,如下所示:

ensemblgb = FTP('ftp.ensemblgenomes.org')
ensemblgb.login()
ensemblgb.cwd("pub/fungi/current/genbank/")
ensemblgb.retrbinary('RETR ' + gbname, open(gbname, 'wb').write)
ensemblgb.quit()

ensemblgff = FTP('ftp.ensemblgenomes.org')
ensemblgff.login()
ensemblgff.cwd("pub/fungi/current/gff/")
ensemblgff.retrbinary('RETR ' + gffname, open(gffname, 'wb').write)
ensemblegff.quit()

但是,我想知道一旦我将目录更改为 "pub/fungi/current/genbank/",是否可以稍后将其更改为 "pub/fungi/current/gff/"(可能通过中间的根文件夹?)同一个 FTP 会话,不关闭它并打开一个新会话。

干杯,

尼古拉

尝试在路径前加上反斜杠 - 它代表根目录:

ensemblgff.cwd("/pub/fungi/current/gff/")