I want to upload a file on an ftp site if the file is not present. This is a task of a more complex application, but it's not important in this case.
My idea is to check if the file is present with the FTP.size(filename)
method and if the error is 550 (The system cannot find the file specified) upload the file.
My (not working) 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)
The error returned is
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 occured:
AttributeError: 'error_temp' object has no attribute 'errno'
The only solution I found to manage the return code was this:
except ftplib.all_errors, e:
if str(e.args[0]).split(" ", 1)[0] == "550":
print "UPLOAD"
else:
print str(e)
but I suppose there is a best solution, because this work only if the error number is the first word in the exception message.
Thank you in advance!