[FWTools] edit shapefile attributes with Python

Frank Warmerdam warmerdam at pobox.com
Tue Aug 15 11:52:08 EDT 2006


Bryan Keith wrote:
> 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.

Brian,

I'm not sure if this got answered already or not.
Try opening the file in update mode.

eg.

  import ogr
 > #get the stream
 > ogrdriver = ogr.GetDriverByName("ESRI Shapefile")
 > dsRiver = ogrdriver.Open("test_py.shp",1)

Good luck,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | President OSGF, http://osgeo.org



More information about the FWTools mailing list