Is there a way to use a "for" loop to print the success messages for all of the following geoprocesses? In this example, I am clipping the entire river system of Kansas to a pre-selected ecoregion, and then calculating the total miles of rivers within that ecoregion.

You can see that I have used the "print" function to display a custom completed message after each geoprocess succeeds, but that can be a lot of work; I think a "for" loop would work better.

You'll notice that I've used the arcpy.GetMessageCount() to print the last message, but it only prints the last message of the last geoprocessing item (I believe)

Here's the code for what I've tried. Once again, I would like to get rid of the print statements underneath each geoprocessing step:

import arcpy, sys
from arcpy import env
env.workspace = "C://whatever_workspace"
env.overwriteOutput = True

inFc = "ks_ecoregions.shp"
clipFc = "ks_major_rivers.shp"
outTable = "out_table"

    #Check out extension
    print "Extension checked out\n"

    #Make feature layer
    arcpy.MakeFeatureLayer_management(inFc, "ks_ecoregions_layer")
    print "Feature Layer Made\n"

    #Select ecoregion
    arcpy.Select_analysis("ks_ecoregions_layer", "ks_ecoregions_select", where_clause = "US_L3NAME = 'Flint Hills'")
    print "Region selected\n"

    #Perform buffer around the selected ecoregion
    arcpy.Buffer_analysis("ks_ecoregions_select", "buffer_layer", "10 Kilometers")
    print "Buffer created\n"

    #Clip rivers to region
    arcpy.Clip_analysis(clipFc, "ks_ecoregions_select", "riverClip")
    print "Clip created\n"

    #Add table to attributes, converting "kilometers" to "miles
    arcpy.AddGeometryAttributes_management("riverClip", "LENGTH", "MILES_US")
    print "Table added\n"

    #Get summary of total miles of rivers in selected region
    arcpy.Statistics_analysis("riverclip", outTable, [["LENGTH", "SUM"]])
    print "Summation computed\n"

    #Delete intermediate data
    print "Intermediate feature class deleted\n"
    print "Intermediate feature class deleted\n"
    print "Intermediate feature class deleted\n"

    #Get projection of original dataset
    desc = arcpy.Describe("riverClip")
    print "Data information for original projection: \nName: {0} \nData Type: {1} \nProjection: {2}\n".format(desc.name, desc.datasetType, desc.spatialReference.name)

   #Check extension back in
    print "Extension checked back in"
except arcpy.ExecuteError:

severity = arcpy.GetMaxSeverity()

if severity == 2:
    print "Error occurred: \n{0}".format(arcpy.GetMessages(2))
elif severity == 1:
    print "Warning raised: \n{1}".format(arcpy.GetMessages(1))
    count = arcpy.GetMessageCount()
    print arcpy.GetMessage(count-1)
  • 2
    This looks like a use case for Python (rather than ArcPy) functions so I suggest using Stack Overflow and other resources to learn how to define and call them. Also, I recommend not using try/except in code presented here - see gis.meta.stackexchange.com/questions/4312/…
    – PolyGeo
    Oct 17 '19 at 21:57
  • 1
    I suspect the function you would have to write to accomplish this would not be worth the time, and may not save you any lines of code. There are only 13 print statements. Even if you function ends up being only 5 lines, will it be worth the time spent to reduce your code by 8 lines? You'd also be adding unnecessary complexity to your code.
    – jbalk
    Oct 17 '19 at 23:43

You could use Result.GetMessage/GetMessages, instead of arcpy.GetMessage/GetMessages

For example:

import arcpy

def log(result):
        # Last message

        # Or all messages...
        # print(result.getMessages())



import arcpy

results = []


for result in results:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.