Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – Print successfully if the def has no except errors

I have this code that send files to a ftp server using try and except.

def sendFiles():
    #send a PDF
    try:
        ftp.cwd('/pdf') 
        pdf = "file1.pdf"  # send the file
        with open(pdf, "rb") as file: 
            ftp.storbinary(f"STOR {pdf}", file)  
    except:
        print(colored(255, 0, 0, f"ERROR !!!!!!!! {pdf} was not sent!"))

    #send new POPUP IMAGE
    try:
        ftp.cwd('/image/popup')
        popup = "popup1.jpg" # send the file
        with open(popup, "rb") as file: 
            ftp.storbinary(f"STOR {popup}", file)
    except:
        print(colored(255, 0, 0, f"ERRO !!!!!!!! {popup} was not sent!"))

I need: if there’s no except errors, i got a print "Files sent with success!"

I tryed this at the end but without success. It is always showing me "Files was not sent!, even i dont get a except error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if sendFiles():
    print("\nFiles sent with success!")
else:
    print("\nFiles was not sent!")

Any idea?

>Solution :

You’re not returning any value from sendFiles so that defaults to a value of None which is the same as False in an if expression. Change sendFiles to return a boolean dependent on whether you succeed or not. For example:

def sendFiles() -> bool:
    sentOK = True
    #send a PDF
    try:
        ftp.cwd('/pdf') 
        pdf = "file1.pdf"  # send the file
        with open(pdf, "rb") as file: 
            ftp.storbinary(f"STOR {pdf}", file)  
    except:
        print(colored(255, 0, 0, f"ERROR !!!!!!!! {pdf} was not sent!"))
        sentOK = False

    #send new POPUP IMAGE
    try:
        ftp.cwd('/image/popup')
        popup = "popup1.jpg" # send the file
        with open(popup, "rb") as file: 
            ftp.storbinary(f"STOR {popup}", file)
    except:
        print(colored(255, 0, 0, f"ERRO !!!!!!!! {popup} was not sent!"))
        sentOK = False

    return sentOK

If you have a lot of these files to send, you might find a helper function useful. For example:

def sendFile(filename, dirname):
    try:
        ftp.cwd(dirname) 
        with open(filename, "rb") as file: 
            ftp.storbinary(f"STOR {filename}", file)  
    except:
        print(colored(255, 0, 0, f"ERROR !!!!!!!! {filename} was not sent!"))
        return False
    return True

Then sendFiles simplifies to:

def sendFiles():
    sentOK = True
    sentOK = sentOK and sendFile('file1.pdf', '/pdf')
    sentOK = sentOK and sendFile('popup1.jpg', '/image/popup')
    return sentOK

There is scope for even more simplification there, for example by passing a list of tuples

[('file1.pdf', '/pdf'), ('popup1.jpg', '/image/popup')]

to sendFiles and then just iterating through the list e.g.

def sendFiles(fileList):
    return all(sendFile(file[0], file[1]) for file in fileList)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading