471,330 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

ftplib.nlst gives error on empty directory

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
1 5475
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.

Similar topics

reply views Thread by Mikael | last post: by
2 posts views Thread by Matija Papec | last post: by
10 posts views Thread by gabedog | last post: by
1 post views Thread by Peter A. Schott | last post: by
2 posts views Thread by Harlin Seritt | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.