I have problem with my while loop code. It is not stopping, despite that values used to evaluated the condition to stop the loop are actually met (I can see in in my print statement).

Here is the code:

count_selectionBefore = 0
count_selectionAfter = 1

while (count_selectionBefore != count_selectionAfter):
    count_selectionBefore = arcpy.GetCount_management("inputLayer")
    arcpy.SelectLayerByLocation_management("inputLayer", "BOUNDARY_TOUCHES", "inputLayer","","ADD_TO_SELECTION")
    count_selectionAfter = arcpy.GetCount_management("inputLayer")
    print "count_selectionBefore", count_selectionBefore
    print "count_selectionAfter", count_selectionAfter
    if count_selectionBefore == count_selectionAfter:
        print "loop is over"

print "selection done. Records selected: ", count_selectionBefore

I know that the values of count_selectionBefore and count_selectionAfte are the same from reading them from print statements.

If you can see why my loop isn't stopping, please let me know.

I updated my code using the help below and new code looks like this:

#point to two datasets which are going to be used 
firetrails = r'H:\NetworkAnalyst\TopologyVer4\firetrail_mb.gdb\firetrail_step4Export'
roads = r'H:\NetworkAnalyst\TopologyVer4\mb_outputs.gdb\roads_O_noZnoPnoC' #r'H:\NetworkAnalyst\TopologyVer4\mb_outputs.gdb\roads_all'

#make feature layer from firetrails
arcpy.MakeFeatureLayer_management(firetrails, "inputLayer")
#make feature layer from roads
arcpy.MakeFeatureLayer_management(roads, "sourceLayer")
arcpy.SelectLayerByLocation_management(r"inputLayer", "BOUNDARY_TOUCHES", r"sourceLayer", "", "NEW_SELECTION")
countBefore = 0
countAfter = 1
while (countBefore != countAfter):
    resultBefore = arcpy.GetCount_management("inputLayer")
    countBefore = int(resultBefore.getOutput(0))
    arcpy.SelectLayerByLocation_management("inputLayer", "BOUNDARY_TOUCHES", "inputLayer","","ADD_TO_SELECTION")
    resultAfter = arcpy.GetCount_management("inputLayer")
    countAfter = int(resultAfter.getOutput(0))   
print "selection done. Records selected: ", countBefore

arcpy.CopyFeatures_management ("inputLayer", r'H:\NetworkAnalyst\TopologyVer4\firetrail_mb.gdb\ft_mb_afterIteration')
  • What is the purpose of this code? Commented Mar 10, 2016 at 11:19
  • Hi Alex. The code selects trails connected to roads through select by location, then it selects from trails again but now connected to trails itself (adds to selection) and keeps add to selection until no more segments are added. The goal is to pick all connected trails to use in network building after.
    – lida
    Commented Mar 11, 2016 at 20:10

1 Answer 1


The result from arcpy.GetCount_management isn't what you think it is.

import arcpy
result = arcpy.GetCount_management(r"D:\test.gdb\lines")
print vars(result)

> {'_arc_object': ArcGIS Result Object }

You'll notice it's not a number. This has tripped me up a number of times on other arcpy results. You'll need to get the result out of the returned object before you can compare them with ==:

actualNumber = int(result.getOutput(0))
  • Thank you. I tried it and it worked. Appreciate your help!
    – lida
    Commented Mar 11, 2016 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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