1

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).

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.