1

This question already has an answer here:

My insert cursor failed in insert any rows. After spending time trying to fix something within the main loops, I added the del inCursor after the loop and now the cursor inserts rows. How can something outside and after a loop impact something above?

#------------THIS DIDN'T WORK:
    inCursor = arcpy.da.InsertCursor(fcEmpty,["Shape@","NAME","CO_WI", "CO_NRI","A_NUMBER","LSE_NO","ORIG_LENGTH"])
    with arcpy.da.SearchCursor(fcPolyline,["Shape@","NAME","ORIG_LENGTH"]) as firstCursor:
        for firstRow in firstCursor: # For each feature in input1
            inputFeat = firstRow[0]
            with arcpy.da.SearchCursor(testingINLAND, ["Shape@","CO_WI", "CO_NRI","A_NUMBER","LSE_NO"]) as intersectCursor:
                for intersectRow in intersectCursor: # for each feature from input2
                    # intersect it with the feature from input1
                    intersectFeat = intersectRow[0]
                    print "check"
                    # dimension ==> polygon geometry (where 1 = point, 2 = polyline)
                    clippedFeature = intersectFeat.intersect(inputFeat, 2)
                    print clippedFeature
                    # setup the insert cursor (for the input you previously created)
                    inCursor.insertRow([clippedFeature,firstRow[1],intersectRow[1],intersectRow[2],intersectRow[3],intersectRow[4],firstRow[2]]) # Insert the resulting geometry from INTERSECT geometry method
    del firstRow, firstCursor
    del intersectRow, intersectCursor

#------------THIS DOES WORK:

inCursor = arcpy.da.InsertCursor(fcEmpty,["Shape@","NAME","CO_WI", "CO_NRI","A_NUMBER","LSE_NO","ORIG_LENGTH"])
with arcpy.da.SearchCursor(fcPolyline,["Shape@","NAME","ORIG_LENGTH"]) as firstCursor:
    for firstRow in firstCursor: # For each feature in input1
        inputFeat = firstRow[0]
        with arcpy.da.SearchCursor(testingINLAND, ["Shape@","CO_WI", "CO_NRI","A_NUMBER","LSE_NO"]) as intersectCursor:
            for intersectRow in intersectCursor: # for each feature from input2
                # intersect it with the feature from input1
                intersectFeat = intersectRow[0]
                print "check"
                # dimension ==> polygon geometry (where 1 = point, 2 = polyline)
                clippedFeature = intersectFeat.intersect(inputFeat, 2)
                print clippedFeature
                # setup the insert cursor (for the input you previously created)
                inCursor.insertRow([clippedFeature,firstRow[1],intersectRow[1],intersectRow[2],intersectRow[3],intersectRow[4],firstRow[2]]) # Insert the resulting geometry from INTERSECT geometry method
del inCursor
del firstRow, firstCursor
del intersectRow, intersectCursor

marked as duplicate by PolyGeo arcpy Jan 16 '17 at 21:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • In the non-working code you have not cleaned up the insert cursor object, and this is needed. In both your non-working and working code you have unnecessarily cleaned up some row objects that are already taken care of by the with statement. Without seeing a code snippet rather than a copy/paste of some lines from your code I cannot say more, but I think when objects need and do not need to be deleted is a pure Python topic better researched at Stack Overflow anyway. – PolyGeo Jan 11 '17 at 22:42
  • 1
    It looks like this discusses it somewhat link, but I still don't understand how it helps with the insert (with lock release) when it the del comes after the insert loop is completed. – Rex Jan 11 '17 at 23:02
  • 2
    I know the cursors store all the info in memory while searching the layer. It must not commit the in memory data to the layer unless the cursor is deleted and the lock is released. – jbalk Jan 11 '17 at 23:55
  • 3
    There are intermittent commits with large row counts, but without an explict commit or stream flush, a small row count would not be flushed until stream closure. Might as well ask why a file close writes to disk. – Vince Jan 12 '17 at 0:02
  • 1
    note that it is recommended to work in an edit session when you have several cursos on the same workspace. I cannot tell from your snippet if this is the case. – radouxju Jan 12 '17 at 7:16

Browse other questions tagged or ask your own question.