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)
    feature = layer.GetNextFeature()


1 Answer 1


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

dataSource = driver.Open(fn,1)
  • This solution worked! confirmed from GDAL-PYTHON API. Default option is 0 i.e FALSE, otherwise 1 for TRUE incase updating is required
    – Erick
    Mar 9, 2018 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.