ftplib.nlst gives error on empty directory

L

loial

Trying to use ftplib.FTP.nlst() method to list the files in
a directory on a FTP server.

It works fine except when there are no files in the directory. Then it
gives the error

ftplib.error_perm: 550 No files found.

How can I handle this cleanly?
 
G

Giampaolo Rodola'

Trying to use ftplib.FTP.nlst() method to list the files in
a directory on a FTP server.

It works fine except when there are no files in the directory. Then it
gives the error

ftplib.error_perm: 550 No files found.

How can I handle this cleanly?

That's the response which comes straight from the server and that
causes ftplib to raise the error_perm exception.
imho, the culprit is the server since it shouldn't return that kind of
response which clashes with the RFC-959 standard specification.
Anyway, to avoid that you could just put your code into a try/except
statement:

try:
files = ftp.nlst()
except ftplib.error_perm, resp:
if str(resp) == "550 No files found":
print "Directory is empty."
else:
raise
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top