NameError: name 'att' is not defined when using Python to download Excel Attachment
NameError: name 'att' is not defined when using Python to download Excel Attachment
当使用 Python 2.7 从 Outlook 自动下载 Excel 附件时,我收到一条错误消息:NameError: 名称 'att' 未定义。奇怪的是,代码直到今天都运行良好。
我尝试移动:att.SaveAsFile 到内部循环。
# Imports arcpy library (Python for ArcGIS) and other libraries required
import arcpy
import os
import sys
import datetime
import win32com.client
from win32com.client import Dispatch
import csv
import pandas as pd
import numpy as np
from tempfile import NamedTemporaryFile
import logging
#import string
"""
Part I: Downloads Excel Spreadsheet (Named Test) from Outlook
"""
# Reference:
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
#messages= msg.Attachments
val_date = datetime.date.today()
sub_today = 'Test ' + date_string
att_today = 'Test ' + date_string+'.xlsx'
# Loop through messages and stop at message with today's subject and attachment.
for msg in all_inbox:
if msg.Subject:
new=msg.Subject
if new.find('Test') !=-1 & new.find(date_string) !=-1:
#print("Step 1")
break
for att in msg.Attachments:
if att.FileName:
#print("Step 2")
break
# Change Directory and Save Attachment
os.chdir('My Directory')
att.SaveAsFile(os.getcwd()+'\'+att_today)
logging.info('Finished Attachment Download')
如果附件列表为空,或者设置了 none of att.FileName,则会出现上述错误。有一个很好的 python 技巧可以避免它:您可以将 else
用于 for
循环。如果您遍历集合并且从不点击 break
.
,则 else
中的代码将被执行
示例如下:
log = logging.getLogger(__name__)
for att in msg.Attachments:
if att.FileName:
break
else:
log.error("file is not found")
att = None
if att:
att.SaveAsFile(att_today)
log.info('Finished Attachment Download')
是否只有1个附件?
当使用 Python 2.7 从 Outlook 自动下载 Excel 附件时,我收到一条错误消息:NameError: 名称 'att' 未定义。奇怪的是,代码直到今天都运行良好。
我尝试移动:att.SaveAsFile 到内部循环。
# Imports arcpy library (Python for ArcGIS) and other libraries required
import arcpy
import os
import sys
import datetime
import win32com.client
from win32com.client import Dispatch
import csv
import pandas as pd
import numpy as np
from tempfile import NamedTemporaryFile
import logging
#import string
"""
Part I: Downloads Excel Spreadsheet (Named Test) from Outlook
"""
# Reference:
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
#messages= msg.Attachments
val_date = datetime.date.today()
sub_today = 'Test ' + date_string
att_today = 'Test ' + date_string+'.xlsx'
# Loop through messages and stop at message with today's subject and attachment.
for msg in all_inbox:
if msg.Subject:
new=msg.Subject
if new.find('Test') !=-1 & new.find(date_string) !=-1:
#print("Step 1")
break
for att in msg.Attachments:
if att.FileName:
#print("Step 2")
break
# Change Directory and Save Attachment
os.chdir('My Directory')
att.SaveAsFile(os.getcwd()+'\'+att_today)
logging.info('Finished Attachment Download')
如果附件列表为空,或者设置了 none of att.FileName,则会出现上述错误。有一个很好的 python 技巧可以避免它:您可以将 else
用于 for
循环。如果您遍历集合并且从不点击 break
.
else
中的代码将被执行
示例如下:
log = logging.getLogger(__name__)
for att in msg.Attachments:
if att.FileName:
break
else:
log.error("file is not found")
att = None
if att:
att.SaveAsFile(att_today)
log.info('Finished Attachment Download')
是否只有1个附件?