我想下载的Windows 7从Outlook 2013附件运行下载附件我写了下面的它的功能:“UnboundLocalError”从Outlook 2013时使用不同的参数
def attach(subject,name):
print "Execution begin"
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
val_date = date.date.today()
print "accessing parameters"
sub_today = subject
att_today = name
for msg in all_inbox:
if msg.Subject == sub_today:
print "sub_today"
break
for attachment in msg.Attachments:
if attachment.FileName == att_today:
print "att_today"
print attachment
break
repository='C:\\Users\\tanmay.shrivastava\\Desktop\\Dashboard'
print "repository"
attachment.SaveAsFile(repository + '\\'+att_today)
print "saved"
print "Execution completed"
我叫函数有两个不同的列表:列表1和列表2.
list1=['Hi','cr.txt']
list2=['abc Requirements','cr.txt']
当我使用list1调用它时代码正在工作。
attach(list1[0],list1[1])
但是当我从列表2参数调用它,它会显示错误
attach(list2[0],list2[1])
UnboundLocalError Traceback (most recent call last)
<ipython-input-12-4238cd3c2efd> in <module>()
----> 1 attach(list2[0],list2[1])
<ipython-input-2-3bb862eb2bb6> in attach(subject, name)
26 repository='C:\\Users\\tanmay.shrivastava\\Desktop\\Dashboard'
27 print "repository"
---> 28 attachment.SaveAsFile(repository + '\\'+att_today)
29 print "saved"
30 print "Execution completed"
UnboundLocalError: local variable 'attachment' referenced before assignment
我不知道是什么原因造成的。有人可以帮我吗?
我的猜测是'msg.Attachments'是空的,因此'attachment'从未定义。 –
@AlexFung我通过发送电子邮件给自己并附带必要的附件进行测试。如果邮件不存在,那么我想它会引发一个“AttributeError” –
所以我的解决方案不能避免错误? –