Wouldn’t it be great to get an email once a script has finished letting you know if it worked or failed? Well it’s actually really easy to pull off. Here’s how I did it:
Note for this example you would replace ‘smtp.example.com’ with your email host.
def SendEmail(sender, receivers, message):
import sys, smtplib
smtpObj = smtplib.SMTP('smtp.example.com')
smtpObj.sendmail(sender, receivers, message)
print 'email sent'
print 'error sending'
then in your script you use the code like this:
—script did some stuff—
–script made some output—
message = "my output message" + output #for example. use whatever you want here to convey the info you need
receivers = "firstname.lastname@example.org"
sender = "email@example.com"
SendEmail(sender, receivers, message)
Looking for an easy way to track time in a script? Here’s what I do.
from time import strftime
starttime = time.time()
print '\n--> Starting script at ', strftime("%a, %d %b %Y %H:%M:%S")
#<your script here>
endtime = time.time()
elapsed = endtime - starttime
print '\n\n--> Finished script... ' , strftime("%a, %d %b %Y %H:%M:%S"), '\n ..Elapsed time: ', round(elapsed/60,1), ' minutes.'
--> Starting script at Mon, 16 Jan 2012 08:16:50
--> Finished script... Mon, 16 Jan 2012 08:17:20
..Elapsed time: 0.5 minutes.
In my progression from utter novice programmer to whatever stage you might say I’m at now, I’ve tried several editors. So far my favorite editor is Komodo. Once I started using this software, those pesky newbie mistakes dropped away. Never again did I forget I closing parantheses or bracket. Every if statement ends in a colon and every try is matched with an except. Komodo keeps track of all of these and makes it impossible to miss when you’ve left them out. And the tab guides are indispensible when you are more than a few tabs deep in a loop inside a loop inside a try/except statement. The keyboard shortcuts for indent/outdent and comment/uncomment are life savers too. It’s also possible to wire up Komodo to read and help with predictive text with the Arcpy modules.
One of the very nice things I appreciate about Komodo is the ability to Continue reading
A few seconds is all you need
I ran into an interesting issue the other day while updating some old ArcMap MXD files. The ArcPy script I wrote worked great except for the last file it processed would get corrupted and not open. I pondered for a bit and spoke with my friend who suggested that the script was ending while the last file was saving and thus corrupting the output since the process did not get to finish properly. I added a delay at the end of the process allowing everything to finish up before the script ends. And…it worked!
Getting a script to sit and do nothing may seem counter-intuitive, but sometimes it’s exactly what needs to happen. Here’s how to make a python script wait 6 seconds.
I discovered the necessity of the compact command (arcpy.Compact_management) while creating a geodatabase. In this particular process there is a lot of selecting, saving as new feature classes, unioning, and finally dissolving and deleting unused temp feature classes. By the end I had what I wanted and all the unnecessary data had been removed, however my relatively small geodatabase (4 feature data sets and less than a hundred features total) was s l o w. As in click to browse in ArcCatalog and go get some tea while you wait slow. Continue reading