Bug report #13325

QgsVectorFileWriter doesn't create fields when writing a new shapefile

Added by Alessandro Pasotti about 9 years ago. Updated about 9 years ago.

Status:Closed
Priority:Normal
Assignee:-
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

Description

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()])

History

#1 Updated by Salvatore Larosa about 9 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 about 9 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