I want to upload a file to the ftp site if the file is missing. This is the task of a more complex application, but in this case it is not important. My idea is to check if the file is present using the FTP.size(filename) method, and if the error is 550 (the system cannot find the specified file), upload the file.
My (doesn't work) code:
from ftplib import * ftp = FTP("ftp.test.com", "user", "password") try: ftp.size("123.zip") except ftplib.all_errors, e: if e.errno == 550: print "UPLOAD" else: print str(e)
Error returned:
Traceback (most recent call last): File "<pyshell#106>", line 4, in <module> if e.errno == 550: AttributeError: 'error_perm' object has no attribute 'errno'
or if a temporary error occurs:
AttributeError: 'error_temp' object has no attribute 'errno'
The only solution I found to manage the return code was the following:
except ftplib.all_errors, e: if str(e.args[0]).split(" ", 1)[0] == "550": print "UPLOAD" else: print str(e)
but I think there is a better solution because it only works if the error number is the first word in the exception message.
Thank you in advance!
source share