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 Answer 1
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– standard2014年07月23日 14:46:13 +00:00Commented Jul 23, 2014 at 14:46
-
1If it worked for you, you can accept the Answer (checkmark under the voting buttons) to the Question gets marked as resolved.Erica– Erica2014年07月23日 14:54:22 +00:00Commented Jul 23, 2014 at 14:54
with
should be over further), possibly a paste problem?arcpy.ListFeatureClasses()
?