Nicola Zilio
Nicola Zilio

Reputation: 415

Navigating between different folders on FTP server with Python

I would like to navigate between two different folders on the same FTP server in the same FTP session using Python's ftplib.

The basic script I wrote is as follows (gbname and gffname are assigned):

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()

This script tracebacks at the second cwd with the following error "ftplib.error_perm: 550 Failed to change directory.".

I understand why it tracebacks there and I can solve the issue by initiating two different FTP sessions, as follows:

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()

However, I was wondering whether, once I change directory to "pub/fungi/current/genbank/", it would be possible to change it to "pub/fungi/current/gff/" later (possibly going through the root folder in between?) in the same FTP session, without closing it and opening a new one.

Cheers,

Nicola

Upvotes: 0

Views: 3330

Answers (1)

fomars
fomars

Reputation: 140

Try prepending the backslash to the path - it stands for the root directory:

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

Upvotes: 1

Related Questions