2

I'm still pretty new to using cursors. What I'd like to do is create a list of shapefiles. Within those shapefiles I would like to populate a field "NAME" with the shapefile name less the .shp extension. I am using 10.1 and my code is below:

bufferCalc = arcpy.ListFeatureClasses("*.shp")
for calc in bufferCalc:
NOTE: This space contains calculations for other fields
with arcpy.da.UpdateCursor(calc, ("PROJNAME")) as cursor:
    for shp in arcpy.ListFeatureClasses():
        cursor.updateRow(os.path.splitext(shp))

The other calculations I need run smoothly however, the error I get when the cursor is ran states "StopIteration: iteration not started". This has got to be something simple I'm doing wrong but can't seem to figure it out.

  • 1
    Indentation is possibly an issue (with should be over further), possibly a paste problem? – Erica Jul 23 '14 at 14:26
  • Good point, it is not a paste problem. When I indent further I get the 'Unexpected Indent' error. Is the cursor something I need to remove from the for loop and create a separate process for? – standard Jul 23 '14 at 14:30
  • 1
    Why do you have 2 arcpy.ListFeatureClasses()? – Paul Jul 23 '14 at 14:32
  • I removed the second ListFeatureClasses. Not sure why I had that in there, I believe I had copied the code from elsewhere. – standard Jul 23 '14 at 14:36
5

I think the largest problem was with the structure of the update cursor. Also, you have two ListFeatureClasses (you only need one to create the list and start the for loop).

Try this:

bufferCalc = arcpy.ListFeatureClasses("*.shp")
for calc in bufferCalc:
    # pull out the shapefile name
    shpName = os.path.splitext(calc)[0]
    # define update cursor
    with arcpy.da.UpdateCursor(calc, ("PROJNAME")) as cursor:
        for row in cursor:
            # set PROJNAME to the shapefile name for each row
            row[0] = shpName
            cursor.updateRow(row) 
  • Thanks @Erica, that worked perfectly. I'm still learning this stuff and cursors is something I need to get a better grasp of. Thanks again. – standard Jul 23 '14 at 14:46
  • 1
    If it worked for you, you can accept the Answer (checkmark under the voting buttons) to the Question gets marked as resolved. – Erica Jul 23 '14 at 14:54

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.