I have a small Python script which is supposed to delete a shapefile in case the file is empty.

import sys
from osgeo import ogr

drv = ogr.GetDriverByName("ESRI Shapefile")
ds = drv.Open(inData)
if ds is None:
    print "Could not open file."

lyr = ds.GetLayer()  

if lyr.GetFeatureCount() == 0:

everything seems to work fine. The shapefile consists of

  • shapefile.dbf
  • shapefile.prj
  • shapefile.shp
  • shapefile.shx

whereas only the *.prj and *.shx get deleted. the *.dbf and *.shp are untouched. Is there anything I am missing?


found the problem:

the files were still busy from the check if it contains geometries.


Setting both variables = None before actually deleting the file solves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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