Reputation: 4159
Do the retrbinary()
and storbinary()
functions in ftplib
raise exceptions if transfer not successful (or do I need to explicitly check for this)?
Eg. I currently have code that does...
ftp = ftplib.FTP(<all the connection info>)
try:
fd = open(MYFILE, 'rb')
res = ftp.storbinary("filename.csv", fd)
print(str(res))
fd.close
except:
print("Failed to upload file")
I notice that 1) the type
of the return value from storbinary
is just a str
and 2) it reads "266 Transfer Complete" when successfully uploaded file to the ftp location. If there is a problem during uploading, will the except section of the code get triggered (ie. will the storbinary
function throw an exception) OR do I need to explicitly check the string literal response like...
ftp = ftplib.FTP(<all the connection info>)
try:
fd = open(MYFILE, 'rb')
res = ftp.storbinary("filename.csv", fd)
print(str(res))
fd.close
if str(res) is not "226 Transfer Complete":
raise Exception
except:
print("Failed to upload file")
...? Is there another more conventional way to catch this? (I have the same question for ftplib
's retrbinary
function as well).
Upvotes: 1
Views: 626
Reputation: 202534
Yes, they will throw an exception, if they get an error response from the server or if the connection is lost unexpectedly.
Though note that with FTP protocol, in some cases, it's not always possible to tell that the transfer has failed.
Upvotes: 2