[QGIS-Developer] deleteShapefile in pyqgis

pierluigi de rosa pierluigi.derosa at gmail.com
Tue Feb 22 09:44:10 PST 2022


Yes I'm on Windows.
On Linux the code works fine.
P

Il Mar 22 Feb 2022, 18:25 Etienne Trimaille <etienne.trimaille at gmail.com>
ha scritto:

> Hi,
>
> Are you on Windows ?
>
> It's long-standing issue on Windows IIRC :
>
> https://gis.stackexchange.com/questions/365713/deleting-shapefile-python-lock-qgis-3-10
> or other links.
>
> Maybe
>  lyr = QgsProject.instance().mapLayersByName(diff_layer)[0]
> QgsProject.instance().removeMapLayer(lyr.id())
> lyr = None
> del lyr
>
> But not a real answer ...
>
> FYI, a nice helper to know side car files:
>
> https://qgis.org/api/classQgsFileUtils.html#a8db232d5063ae13133fa2d3351be1b9c
>
> Le mar. 22 févr. 2022 à 18:07, pierluigi de rosa <
> pierluigi.derosa at gmail.com> a écrit :
>
>> Dear all,
>>
>> I have some troubles wher I use in a pyqgis the function
>> QgsVectorFileWriter.deleteShapeFile(file_path)
>> here below the example:
>>
>> def removeLayer(path_layer):
>>     # remove layer from TOC if already loaded
>>     basefile = os.path.basename(path_layer)
>>     diff_layer = os.path.splitext(basefile)[0]
>>     directory = os.path.dirname(path_layer)
>>     extensions = ["shp", "shx", "dbf", "prj", "sbn", "sbx", "fbn", "fbx", "ain", "aih", "ixs", "mxs", "atx", "xml",
>>                   "cpg", "qix"]
>>     output_path=path_layer
>>     if os.path.exists(path_layer):
>>         if len(QgsProject.instance().mapLayersByName(diff_layer)) > 0:
>>             lyr = QgsProject.instance().mapLayersByName(diff_layer)[0]
>>             print('renaming layer1: ', lyr.id())
>>             QgsProject.instance().removeMapLayer(lyr.id())
>>
>>         QgsVectorFileWriter.deleteShapeFile(path_layer)
>>
>> Often some handles still remains and I'm not able to delete .dbf and .shp
>> files
>> How can I solve this error?
>>
>> Thanks
>> Pierluigi
>>
>> --
>> Ing. Pierluigi De Rosa (PhD in Earth Science)
>> Contract Professor of Geographic Information System at University of
>> Perugia
>> cel: 3497558268 / fax: 075 7823038
>> skype: pierluigi.derosa
>> _______________________________________________
>> QGIS-Developer mailing list
>> QGIS-Developer at lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20220222/5a1463d8/attachment-0001.html>


More information about the QGIS-Developer mailing list