I am creating a standalone PyQGIS application and is met with a problem.
When a shapefile is shown on my canvas, I am unable to delete the corresponding .shp and .dbf files in my directory.
This is the error I get:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Data\\tmpy187h2zs\\0-Boundary\\0-Boundary.dbf'
This does not happen when I try to close raster files.
I am using PyQt for the GUI. And there's a workaround to this problem by having 2 buttons. Remove from canvas and Delete.
After pressing the Remove from canvas button, I am able to delete the layer. However, when I combine both of these buttons together, an error occurs.
Here is the function for remove layer and delete:
Remove layer:
def removeAllLayers(Main):
layers = Main.layers
registry = QgsProject.instance()
if len(layers) != 0:
layers = []
registry.removeAllMapLayers()
Main.canvas.refresh()
Delete:
def deleteFiles(Main):
for files in os.listdir(Main.tempPath + '/' + button):
os.remove(Main.tempPath + '/' + button + "/" + files)
print("Files Deleted")
When combined:
def removeAndDeleteFile(Main):
removeAllLayers(Main)
deleteFiles(Main)
and these functions are just embedded into QPushButton:
pushButtonRemoveFromCanvas.clicked.connect(lambda: removeAllLayers(self))
pushButtonDeleteFiles.clicked.connect(lambda: deleteFiles(self))
pushButtonRemoveAndDelete.clicked.connect(lambda: removeAndDeleteFile(self))
This works:
- Click pushButtonRemoveFromCanvas
- Click pushButtonDeletFiles
This does not work:
- Click pushButtonRemoveAndDelete Returns PermissionError as mentioned above
The main problem stems from that the .shp and .dbf files are not deleted. I have browsed other threads such as can't erase dbf and shp pyQGIS and Deleting raster file after doing some processing in QGIS? but they do not directly answer the question and is a workaround specific to their question.
I have been on this problem for weeks and is unable to solve it, wonder if anyone can help me.
gc.collect()
) between the two calls.