Bug report #13325

QgsVectorFileWriter doesn't create fields when writing a new shapefile

Added by Alessandro Pasotti over 8 years ago. Updated over 8 years ago.

Category:Data Provider
Affected QGIS version:master Regression?:No
Operating System:Linux Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:21379


This snippet does not work as expected

fields = QgsFields()
fields.append(QgsField('name', QVariant.String, 'name', 10))
w = QgsVectorFileWriter('points.shp', 'utf-8', fields, QGis.WKBPoint, QgsCoordinateReferenceSystem(4326))

# An existing 'name' field is expected

layer = QgsVectorLayer('points.shp', 'points', 'ogr')
assert('name'  in [f.name() for f in layer.pendingFields()])


#1 Updated by Salvatore Larosa over 8 years ago

you have to delete the writer in order to close the writing of the shapefile. This is mandatory right now.

if w.hasError():
    print w.errorMessage()

del w

#2 Updated by Alessandro Pasotti over 8 years ago

  • Resolution set to invalid
  • Status changed from Open to Closed

Thank you! I've updated the cookbook (deleted "optional").

Also available in: Atom PDF