Retrieving tool messages with GetMessages()

ArcPy includes a GetMessages() function that you can use to retrieve messages generated when an ArcGIS tool is executing. Messages can include informational messages, such as the start and ends times of a tool execution as well as warnings and errors, which can result in something less than the desired result or complete failure of the tool to execute to completion.

Getting ready

During the execution of a tool, various messages are generated. These messages include informational messages, such as the start and end times of a tool execution, parameter values passed to the tool, and progress information. In addition to this, warnings and errors can also be generated by the tool. These messages can be read by your Python script, and your code can be designed to appropriately handle any warnings or errors that have been generated.

ArcPy stores the messages from the last tool that was executed and you can retrieve these messages using the GetMessages() function, which returns a single string containing all messages from the tool that was last executed. You can filter this string in terms of severity to return only certain types of messages such as warnings or errors. The first message will always include the name of the tool executed, and the last message is the start and end time.

In this recipe, you will add a line of code to the except statement, which will print more descriptive information about the current tool run.

How to do it…

Follow these steps to learn how to add a GetMessages() function to your script that generates a list of messages from the tool that was last executed:

  1. If necessary, open the C:ArcpyBookCh11ErrorHandling.py file in IDLE.
  2. Alter your script to include the GetMessages() function:
    import arcpy
    try:
      arcpy.env.workspace = "c:/ArcpyBook/data"
      arcpy.Buffer_analysis("Streams.shp","Streams_Buff.shp")
    except:
      print(arcpy.GetMessages())
  3. You can check your work by examining the C:ArcpyBookcodeCh11ErrorHandling3.py solution file.
  4. Save and run the script. This time, the error message should be much more informative. Also notice that there are other types of messages that are generated including the start and end times of the script's execution:
    Executing: Buffer c:/ArcpyBook/dataStreams.shp c:/ArcpyBook/dataStreams_Buff.shp # FULL ROUND NONE #
    Start Time: Tue Nov 13 22:23:04 2012
    Failed to execute. Parameters are not valid.
    ERROR 000735: Distance [value or field]: Value is required
    Failed to execute (Buffer).
    Failed at Tue Nov 13 22:23:04 2012 (Elapsed Time: 0.00 seconds)
    

How it works…

The GetMessages() function returns all the messages generated by the last tool that was run. I want to emphasize that it only returns messages from the last tool that was run. Keep this in mind if you have a script with multiple tools that are being run. Historical tool messages are not accessible through this function. However, there is a Result object that you can use if you need to retrieve historical tool messages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset