ftplib.nlst gives error on empty directory

Giampaolo Rodola' gnewsg at gmail.com
Fri Dec 7 13:25:47 EST 2007


On 7 Dic, 10:42, loial <jldunn2... at googlemail.com> wrote:
> 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
...



More information about the Python-list mailing list