[GIS] How to change the field value of a Shapefile using GDAL/OGR

fields-attributesgdalogr

I am trying to change the field value of a shapefile. However it doesn't change the value. What's wrong with my code?

import ogr

driver = ogr.GetDriverByName('ESRI Shapefile')
fn = 'dist.shp'
dataSource = driver.Open(fn, 0)

layer = dataSource.GetLayer()
feature = layer.GetNextFeature()

dist = 233

while feature:
    feature.SetField("dist", dist)
    layer.SetFeature(feature)
    feature = layer.GetNextFeature()

dataSource.Destroy()

Best Answer

The second argument in Open specifies if the data can be updated (written to). Try:

dataSource = driver.Open(fn,1)