Python Error Catching & FTP

You write

except Exception, e:  #you can specify type of Exception also
   print str(e)

I can't do

except: ftplib.all_errors

Of course not, that's simply bad syntax! But of course you can do it with proper syntax:

except ftplib.all_errors:

i.e., the colon after the tuple of exceptions.

How can I retrieve more specific information on the error? Perhaps the error code?

except ftplib.all_errors as e:
  errorcode_string = str(e).split(None, 1)[0]

E.g., '530' will now be the value of errorcode_string when the complete error message was '530 Login authentication failed'.

You can find the rest of the exception in the docs.

Tags:

Python

Ftp