Select to view content in your preferred language

E mail NameError Error Python

809
1
01-24-2012 06:15 AM
davidmetzler
Emerging Contributor
Hello,
I am sort of new to python and I am encountering an error I just cant get past. This script copies a shapefile to a new location. due to locks I have to run it automatically when services are stopped. I want the script to email me the specific error if there is one or email me a message saying it completed successfully. here is the code:


import os
import arcpy
import smtplib
import string

arcpy.env.overwriteOutput = True
try:
arcpy.CopyFeatures_management ('\\\\petrots\\data\\PAM\\PFS-Survey\\Data\\xxxxx.shp', '\\\\PETROTS\\arcgisproject\\PAM\\Clients\\PFSSurvey_v1\\Data\\xxx.shp')
except Exception as e:
print e.message
errormessage = e.message

try:
to = 'me@email.com'
gmail_user = 'username'
gmail_pwd = 'password'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + errormessage
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()
except NameError:
to = 'me@email.com'
gmail_user = 'username'
gmail_pwd = 'password'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + 'all is good'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()

When I run it everything is smooth if there is an error however if all is good I get this:

Traceback (most recent call last):
File "P:\Scripts\copy to Y test.py", line 14, in <module>
errormessage = e.message
AttributeError: 'str' object has no attribute 'message'



I thought the except clause would take care of this however it just isn't getting the job done.

Any help would be appreciated.

Cheers,
Dave Metzler
Tags (2)
0 Kudos
1 Reply
StacyRendall1
Frequent Contributor
Dave, the try...except clause you had highlighted was further down the code than where the error was occurring...

Your error is telling you that the object e (which is a string - 'str') has no attribute message. This is possibly because message is the old method (deprecated as of Python 2.6).

The new method goes something like this:
import arcpy

try:
 arcpy.ImportToolbox("popeye")
except Exception as e:
 errormessage = str(type(e)) + '\n'
 for arg in e.args:
  errormessage += '  ' + str(arg) + '\n'
 print errormessage

Firstly it gets the type of the error, then on a new line returns any arguments associated with the error...

This should work:
import arcpy
import smtplib

arcpy.env.overwriteOutput = True
try:
 arcpy.CopyFeatures_management ('\\\\petrots\\data\\PAM\\PFS-Survey\\Data\\xxxxx.shp', '\\\\PETROTS\\arcgisproject\\PAM\\Clients\\PFSSurvey_v1\\Data\\xxx.shp')
except Exception as e:
 errormessage = str(type(e)) + '\n'
 for arg in e.args:
  errormessage += '  ' + str(arg) + '\n'

 print 'Operation failed, sending error message:\n', errormessage

 to = 'me@email.com'
 gmail_user = 'username'
 gmail_pwd = 'password'
 smtpserver = smtplib.SMTP("smtp.gmail.com",587)
 smtpserver.ehlo()
 smtpserver.starttls()
 smtpserver.ehlo()
 smtpserver.login(gmail_user, gmail_pwd)
 header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
 print header
 msg = header + errormessage
 smtpserver.sendmail(gmail_user, to, msg)
 print 'done!'
 smtpserver.close()


Let me know how you get on.
0 Kudos