0

I am working on a Python script that will count how many selected features there are and if there are it will make a feature layer from them. If the script runs and finds there are 0 selected records it should print 'NO SELECTED RECORDS'. However when I run my script it is ignoring my If Then statement and creating a duplicate layer of "Locates", but still telling me I have no records selected.

Here is my code:

import arcpy

totalCount = arcpy.GetCount_management("Locates")
print 'Total Record Count = ' + `totalCount`
try:
    arcpy.SelectLayerByLocation_management("Locates","INTERSECT","Boxes",0,"NEW_SELECTION","INVERT")
    arcpy.SelectLayerByLocation_management("Locates","INTERSECT","Lines",0,"REMOVE_FROM_SELECTION")
    ##GET COUNT OF SELECTED RECORDS
    selectedCount = len(arcpy.Describe("Locates").FIDSet.split('; '))-1
    print 'Selected Records Count' + `selectedCount`
    if selectedCount != totalCount:
        ##IF SELECTED ROWS NOT EQUAL TO 0 THEN MAKE FEATURE LAYER ELSE PRINT 'NO LOCATES TO CLOSE'
        arcpy.MakeFeatureLayer_management("Locates","NeedToCLOSE")
        for row in arcpy.SearchCursor("NeedToCLOSE"):
            print 'Close Work Order #' + `row.WorkOrderID`
    else:
        print 'No Locates to close'
except:
    print(arcpy.GetMessages())

What do I need to change in my code for this to work?

  • 1
    See help on getCount. You need int(result[0]) to treat it as integer. – FelixIP Aug 16 '18 at 22:00
2

As FelixIP comments, you need to convert your getCount() result to an integer

result = arcpy.GetCount_management("Locates")
totalCount = int(result[0])

See the code sample in Get Count - Help

  • Do you know how I would alter my code to convert it into a script tool? – cbunn Aug 20 '18 at 14:23

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.