你没有从中返回任何值
sendFiles
所以默认值为
None
这和
False
在一个
if
表示改变
发送文件
根据是否成功返回布尔值。例如:
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
如果你有很多这样的文件要发送,你可能会发现一个助手函数很有用。例如:
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
然后
发送文件
简化为:
def sendFiles():
sentOK = True
sentOK = sentOK and sendFile('file1.pdf', '/pdf')
sentOK = sentOK and sendFile('popup1.jpg', '/image/popup')
return sentOK
这里还有进一步简化的余地,例如通过传递元组列表
[('file1.pdf', '/pdf'), ('popup1.jpg', '/image/popup')]
到
发送文件
然后只是反复浏览列表,例如。
def sendFiles(fileList):
return all(sendFile(file[0], file[1]) for file in fileList)