我有一段代码,可以使用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!")
知道吗?