By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

ftplib.nlst gives error on empty directory

P: n/a
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?

Dec 7 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 7 Dic, 10:42, loial <jldunn2...@googlemail.comwrote:
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
....
Dec 7 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.