を実行したとき、私はWindows 7の上の見通し2013年から添付ファイルをダウンロードしようとしている私はそれのための機能を以下に書いた:「UnboundLocalError」異なるパラメータ
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の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"を発生させると思います –
私の解決策はエラーを回避しないのですか? –