[FWTools] edit shapefile attributes with Python

Bryan Keith bryan at geomega.com
Thu Aug 10 10:21:51 EDT 2006


Hello,

I'm simply trying to edit the attributes of a shapefile with Python.  I 
have no trouble creating a new shapefile, creating new features, and 
adding them to the shapefile.  However, now I just want to edit 
attributes of the dbf.   Here's my simple script:

import ogr
#get the stream
ogrdriver = ogr.GetDriverByName("ESRI Shapefile")
dsRiver = ogrdriver.Open("test_py.shp")
mRiverLayer = dsRiver.GetLayerByName("test_py")
mFeature = mRiverLayer.GetFeature(0)
iIndex = mFeature.GetFieldIndex("RIVER_MILE")
mFeature.SetField(iIndex, 100.1)
mRiverLayer.SetFeature(mFeature)

which gives me this error:

ERROR 3: Error in fseek() or fwrite() writing object to .shp file.

Is attribute updating supported in shapefiles?  What am I doing wrong? 
Thanks for the help.

Bryan

Bryan Keith
GIS Specialist
Geomega, Inc.
Boulder, CO, USA


More information about the FWTools mailing list