Saving Processing Results to a text file

1686
14
09-02-2011 04:28 AM
ChristinaGnadinger
New Contributor II
I am hoping that this is a simple question.

I'm running python scripts through the Windows scheduled tasks and would like to come in the next morning to make sure the processes have ran completely, and without errors. Is there a command line I can insert into the python scripts that can output these result to a text file for me?

Thank you in advance for your help!
Christina
Tags (2)
0 Kudos
14 Replies
JakeSkinner
Esri Esteemed Contributor
You can use the python open function to accomplish this.  You could write to a text file whether a script/tool was executed successfully or not.  Below is an example.  If the text file does not exist, it will automatically be created.

import arcpy, os
from arcpy import env
env.workspace = r"c:\temp\python\test.gdb"

outtable = open(r"C:\temp\results.txt", "w")

try:
    arcpy.FeatureClassToFeatureClass_conversion("Airports", env.workspace, "Airports2")
    outtable.write("Converted successfully")
except arcpy.ExecuteError:
    outtable.write(arcpy.GetMessages())

outtable.close() 
0 Kudos
ChristinaGnadinger
New Contributor II
Thank you so much .. that is working perfectly for me!

On a side note does anyone know of any good websites, or any good books related to both Python and ArcGIS? One of these days I need to breakdown and just learn it I think.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I highly recommend the class 'Introduction to Geoprocessing Scripts Using Python'.  It gives a great overview of using python with ArcGIS and the new Esri-developed Python site package Arcpy.
0 Kudos
ChristinaGnadinger
New Contributor II
Thank you!! I will definitely look into that!
0 Kudos
MikeMacRae
Occasional Contributor III
As far as books, I have a few kicking around the office, but when I need something dumbed down for me a little, I tend to hit up my copy of "Python for Dummies" Like most Dummies books, it really helps you get going.
0 Kudos
MalcolmParnell2
New Contributor
Thank you so much .. that is working perfectly for me!


At the risk of complicating things, another option is to get python to send you an email if it fails. Some examples are here .

I don't know enough about python to get this working at this stage, but it's certainly something I would like to implement for scheduled tasks.

If you do happen to go this way I'll trade you: my idea for your code . 😄
0 Kudos
StacyRendall1
Occasional Contributor III
Here is a way you can do it; writes the text file and sends an email. The trickiest bit is getting it to work with your specific SMTP email server. If you have gmail it's easy as pie; if not - google around...
import smtplib
import arcpy
 
### This is the module that does the sending - you should only need to edit the top four active lines
def sendResultEmail(msgContents, success_TF):
 to = 'receiver@email.com'
 send_username = 'sender@email.com'
 send_password = 'password' # warning - will just be an unencrypted string - so be careful not to leave this file lying around!
 
 # This is the tricky bit - here are the settings for gmail (which will only work if your sender is gmail and you have a gmail account).
 # If you are using something else, google around smtplib and the email service you use.
 # I would suggest gmail is probably a pretty good start if you have an account...
 smtpserver = smtplib.SMTP("smtp.gmail.com",587)

 
 if success_TF:
  subject = 'Arcpy testing results: script SUCCESS.'
 else:
  subject = 'Arcpy testing results: script FAILURE.'
  
 smtpserver.ehlo()
 smtpserver.starttls()
 smtpserver.ehlo
 smtpserver.login(send_username, send_password)
 
 header = 'To:' + to + '\n' + 'From: ' + send_username + '\n' + 'Subject:' + subject + '\n'

 msg = header + '\nArcpy results: \n\t' + msgContents + '\n'
 
 smtpserver.sendmail(send_username, to, msg)
 
 arcpy.AddMessage('Results email sent!')
 
 smtpserver.close()


### Here is the geoprocessing code; add to strResult every time you need to add something to the message (i.e. completion times)
### In a complex code this could be a lot of information, so seperate with '\n' to force new lines in the text...

inFC = arcpy.GetParameterAsText(0)
outLoc = arcpy.GetParameterAsText(1)
outFC = arcpy.GetParameterAsText(2)

success = False # assume failure (changed to True if script is successful)
strResult = '' # start empty string - write to this, for saving to text file and sending to email

try:
 arcpy.FeatureClassToFeatureClass_conversion(inFC, outLoc, outFC)
 strResult += "Converted successfully \n"
 success = True
except arcpy.ExecuteError:
 strResult += arcpy.GetMessages()

arcpy.AddMessage(strResult)

# Now write results to the text file
outtable = open(r"C:\temp\results.txt", "w")
outtable.write(strResult)
outtable.close()

# Now send the email. This line calls the module above, passing it the string and the T/F success variable...
sendResultEmail(strResult, success)


Let me know how you get on.
0 Kudos
ChristinaGnadinger
New Contributor II
I'll have to check out the Python for Dummies book .. sometimes I like those better since they're a bit less like reading stereo instructions at times 🙂 .. I've been rooting through the Python online reference PDFs and they are not the most interesting reads out there. Thanks for the recommendation!

Also .. I was thinking about the whole email concept but decided it was way over my head .. however you have me interested in doing it now. I may look into that and see if I can make it work on my end.. If so I'll definitely give you all an update! Thanks for the start up on that 😃
0 Kudos
StacyRendall1
Occasional Contributor III
On a side note does anyone know of any good websites, or any good books related to both Python and ArcGIS? One of these days I need to breakdown and just learn it I think.


I'm actually trying to develop a library of tutorials and examples on my blog, which I hope one day will be a good resource for people! At the moment it only has very basic stuff and some very specific advanced stuff. I would really like to hear what people are struggling with so I can build some tutorials in those areas.

I'm currently planning a Python Essentials for Arcpy which will cover some of the basics, especially the handy little things that Python can do which make scripts better than modelbuilder models, and an Arcpy Code Optimization tutorial which will cover the methodological process of developing geoprocessing scripts, and how to leverage Python functionality to improve speed (and reliability).

So, if you have any specific questions, comments on the proposed tutorials above, or things you would like information on, please let me know and I'll try to help!
0 Kudos