Py学习  »  Python

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

Lucas Fernandes • 3 年前 • 1521 次点击  

我有一段代码,可以使用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
 
1521 次点击  
文章 [ 3 ]  |  最新文章 3 年前