0

I'm trying to iteratively select and export a layer using da.SearchCursor, but the loop stops after the first iteration. Here is the code:

arcpy.env.workspace = ScratchGDB #
infc= "PA" 
field = 'UniqueID'
arcpy.MakeFeatureLayer_management(infc, "infcLyr")

for row in arcpy.da.SearchCursor(infc, field): 
    iterateValue = row[0]
    exp = field + " = '" + iterateValue + "'"
    outfc = ScratchGDB + iterateValue
    if arcpy.Exists(outfc):
        print ("skip - output exists") # skip row if it has been exported already
    else:
        selectRow = arcpy.SelectLayerByAttribute_management("infcLyr", "NEW_SELECTION", exp)
        arcpy.CopyFeatures_management("infcLyr", outfc)

However, when I use a similarly structured code, but instead of exporting, I just printed the output path, the loop worked fine:

arcpy.env.workspace = ScratchGDB #
infc= "PA" 
field = 'UniqueID'
arcpy.MakeFeatureLayer_management(infc, "infcLyr")

for row in arcpy.da.SearchCursor(infc, field): 
    iterateValue = row[0]
    exp = field + " = '" + iterateValue + "'"
    outfc = ScratchGDB + iterateValue
    if arcpy.Exists(outfc):
        print ("skip - output exists") # skip row if it has been exported already
    else:
        print(outfc) 

Update: I tried to set the arcpy.env.overwriteOutput to FALSE following the suggestion by @Emil Brundage. The loop still stops after both SelectLayerByAttribute_management and CopyFeatures_Management run for the first time. Note that the first part of the if statement loops with no problem; i.e., if there are multiple existing outputs, the loop will skip all of them. It only stops after the select and copy tools run through their first input row.

4
  • Try adjusting your whereclause : exp = """{} = {}""".format(arcpy.AddFieldDelimiters(infc, field), iterateValue) (or exp = """{} = '{}'"""format(arcpy.AddFieldDelimiters(infc, field), iterateValue) if your field is string).
    – SMiller
    Commented Sep 27, 2019 at 15:38
  • @smiller thank you! Updated, and it still doesn't loop :(
    – ElTajibo
    Commented Sep 27, 2019 at 17:38
  • You're doing evil things with the selection buffer of a layer while iterating the layer. I should certainly hope that doesn't work.
    – Vince
    Commented Feb 25, 2020 at 22:51
  • It would make more sense to first determine your unique values using the cursor (hint: use a set), then loop over those values to select from your layer and export
    – mikewatt
    Commented Feb 26, 2020 at 1:30

1 Answer 1

1

It looks like you have arcpy.env.overwriteOutput set to True. With each iteration you are copying over whatever you have ExportBuffer set as. Currently you have a single feature class that's been copied over with each iteration, giving the appearance that the loop only iterated one row. You'll need to update your output feature class name in variable ExportBuffer with each iteration to a unique name to prevent copying over the previous loop's results.

1
  • My apologies - I changed some parameters from my original code before posting this question but forgot to change ExportBuffer to 'outfc'; it's now updated in my question.
    – ElTajibo
    Commented Sep 27, 2019 at 15:15

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.