社区所有版块导航
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
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python-如果def没有异常错误,则成功打印

Lucas Fernandes • 3 年前 • 1519 次点击  

我有一段代码,可以使用try和except将文件发送到ftp服务器。

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!"))

我需要:如果没有错误,我会打印“文件发送成功!”

最后我尝试了一下,但没有成功。它总是显示“文件未发送!”,即使我没有收到异常错误:

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

知道吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132784
 
1519 次点击  
文章 [ 3 ]  |  最新文章 3 年前