<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-15">
</head>
<body bgcolor="#FFFFFF" text="#006600">
<p>Dear List,</p>
I'd like to remove an existing file (see [tempOutput]) after it was
used by a geoalgorithm. The geoalgorithm works perfectly but after
its execution, the file [tempOutput] is locked.
<p>Here a simple test script that returns "[Error 32] The process
cannot access the file because it is being used by another
process: 'D:/test_alg/map_to_clip.tif'":</p>
<p>import processing<br>
import os<br>
<br>
tempOutput = 'D:/test_alg/map_to_clip.tif'<br>
maskFN = 'D:/test_alg/mask.shp'<br>
output = 'D:/test_alg/clipped_map.tif'<br>
<br>
algRes = processing.runalg("gdalogr:cliprasterbymasklayer",<br>
tempOutput,<br>
maskFN,<br>
"-9999",False,False,False,5,4,75,6,1,False,0,False,"",<br>
output)<br>
<br>
try:<br>
os.remove(tempOutput)<br>
except Exception as e:<br>
print e</p>
<p>I also found a solution on the web [1] but it doesn't work for me
:( If I understand correctly, the trick is to load the file as
layer and then release it. Here its implementation:</p>
<p>import processing<br>
import os<br>
<br>
tempOutput = 'D:/test_alg/map_to_clip.tif'<br>
maskFN = 'D:/test_alg/mask.shp'<br>
output = 'D:/test_alg/clipped_map.tif'<br>
<br>
algRes = processing.runalg("gdalogr:cliprasterbymasklayer",<br>
tempOutput,<br>
maskFN,<br>
"-9999",False,False,False,5,4,75,6,1,False,0,False,"",<br>
output)<br>
<br>
try:<br>
# add to the map<br>
tempLay = QgsRasterLayer(tempOutput, "tempLay")<br>
if not tempLay.isValid(): raise Exception("Failed to load
tempLay")<br>
QgsMapLayerRegistry.instance().addMapLayer(tempLay) #!!!!<br>
<br>
layermap = QgsMapLayerRegistry.instance().mapLayers()<br>
for i, layer in layermap.iteritems():<br>
print 'layername',layer.source() <br>
if layer.source() == tempOutput:<br>
# Remove layer references<br>
QgsMapLayerRegistry.instance().removeMapLayer(layer.id())
#!!!!<br>
# Delete the file<br>
os.remove(tempOutput)<br>
<br>
except Exception as e:<br>
print e</p>
<p>I didn't find any other solution for the moment. Any suggestion
is really very appreciate!<br>
</p>
<p>Thanks</p>
<p>enrico<br>
</p>
<p><br>
</p>
<p>[1]
<a class="moz-txt-link-freetext" href="https://gis.stackexchange.com/questions/93143/can-not-delete-file-after-using-runalg-function">https://gis.stackexchange.com/questions/93143/can-not-delete-file-after-using-runalg-function</a><br>
</p>
<p><br>
</p>
<p><br>
</p>
</body>
</html>