3

I my plugin I use the runalg function:

processing.runalg("qgis:clip",filepath + "/temp2.shp", Layer, filepath + '/' + layerName)
vlayer = QgsVectorLayer(filepath + '/' + layerName + '.shp',layerName, "ogr")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

Afterwards I want to delete temp2.shp with

os.remove(filepath + "/temp2.shp") 

but I get the error WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'C:/Users/toke.nielsen/Desktop/temp/temp2.shp'

Is there a way to unlock temp2.shp after using runalg? And is there a way to clear all variables and memory when the plugin is done?

I have tried this, but it is not working with QGIS 2.2:

Releasing PyQGIS file locks?

  • I could avoid this, if I somehow could get the path of where my plugin is installed. Then I could make a temp folder which is deleted every time I use the plugin. But is there any command to get the installed path of my own plugin? The path could change from user to user. – Toke Apr 15 '14 at 11:16
3

The solution:

QgsMapLayerRegistry.instance().removeMapLayer(vlayer.id())

before

os.remove()
1

I was stuck here for some time as well until I figured it out. Just to clarify the layer that you want to remove you have to add to the canvas and then remove it. For anyone with same problem look on the answer here, and the rows with "#!!!!"..

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.