社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Jorge Alvarez

Jorge Alvarez 最近创建的主题
Jorge Alvarez 最近回复了
3 年前
回复了 Jorge Alvarez 创建的主题 » Python-如果def没有异常错误,则成功打印

尝试使用 except Exception as e 要在触发异常时捕获异常的属性,可以使用 e.message 看看它是否有帮助。而且 if sendFiles(): print("\nFiles sent with success!") if语句总是会被触发,因为它的唯一条件是函数正在运行,而不是检查是否发送了文件。

也许你可以测试它,返回一个变量。

def sendFiles():
    file_sent = False
    image_sent = False
    #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)  
        file_sent = True
    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)
        image_sent = True
    except:
        print(colored(255, 0, 0, f"ERRO !!!!!!!! {popup} was not 
        sent!"))
    return file_sent, image_sent

file_sent, image_sent = sendFiles()
        
if all([file_sent, image_sent]):
    print("\nFiles sent with success!")
else:
    print("\nFiles was not sent!")