TransWikia.com

Closing QgsRasterLayer's file handler in Python script

Geographic Information Systems Asked by Edgar Szilagyi on December 2, 2020

I’m creating a QgsRasterLayer from temporary file, as result from some processing algorithm.

After that, I no longer can delete file, as expected. But, because It is only a temp file, I need delete It after all my processing is done.

I tried, without success:

layer = QgsRasterLayer("myTempFile.tif")

#Do some work...

del layer

myTempFile.tif still can’t be deleted, but only after closing QGIS.

One Answer

Here is a similar thread which you can also read: Removing a vector layer from QGIS

Try something like this, you will need to adapt it to reflect the layer(s) you are working with.

layer = QgsRasterLayer('path/to/your/raster.tif')

#do the rest of your code

QgsProject.instance().removeMapLayer(layer)

Answered by Erich Purpur on December 2, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP