I have a code that creates some tif images in .qgis2 folder, but I want, in the end, to delete these files. How do I do that?

I try with os.remove but it gives me the error32, that said that the process can´t access to file because this is used by another process.

  • There is probably a lock on the file from the process. Am I correct in assuming you are using qgis?
    – Barbarossa
    Dec 3, 2013 at 16:50
  • i think this is rather a coding question and you get an answer a lot faster if you ask it on stackoverflow: stackoverflow.com/search?q=python+delete+files
    – Curlew
    Dec 3, 2013 at 16:51
  • Yes. I am using qgis. Dec 5, 2013 at 14:44

4 Answers 4


There will be a lock on the file, which will remain until the python process is closed. To get around this, create your TIF images in a subprocess. Then when the subprocess finishes it will release the lock and you can delete the files.

  • 2
    +1 Good answer. Any chance of getting an example code snippet?
    – Aaron
    Dec 3, 2013 at 17:10
  • Better just to close the open file (using whatever methods QGIS provides) before trying to delete it, yes? This isn't a Python problem at all.
    – sgillies
    Dec 4, 2013 at 14:53
  • I didn't open the file. I am developing a qgis plugin and I am importing processing algorithms. The tif images are used internally between this algorithms. But in the end I want to remove all of them. I just want the last tif image created. Dec 5, 2013 at 14:46
  • It is because the Python process created it which is why it has the lock. Dec 5, 2013 at 15:29
  • MappaGnosis, this is not quite true. A lock remains because some code (in pyqgis, I presume) isn't closing opened files properly. It's quite possible to have long running Python processes that don't lock down files any longer than necessary.
    – sgillies
    Dec 5, 2013 at 17:13

If the tif images are referenced in your Python code as variables, you will need to delete the variables once the code has finished running, in order to remove the lock. You can do this with the 'del' command, e.g.

del variableName

This will then enable you to delete the tif files. The following post on Stack Overflow might help with deleting the tif files from within your Python code: https://stackoverflow.com/questions/6996603/how-do-i-delete-a-file-or-folder-in-python

os.remove should do the job once the lock has been removed.

  • So, to unlock the files I have to del the variables right? I did that and I have this error: UnboundLocalError: local variable 'filedir' referenced before assignment The variable for the file path in this case is "filedir". Dec 6, 2013 at 9:52
  • Perhaps if you post a snippet of the code that you are referring to someone here will be able to help?
    – the_bonze
    Dec 6, 2013 at 9:57

the_bonze is right; deleting the variable with the del command kills whatever process is using the file within your routine. You get the "referenced before assignment" error because after you delete the variable, it is not saved.

When you remove the file, you have to specify the path. For example:

del variable



There is an alternative way to delete the unnecessary folder. It overcomes the problem with the locked files because it ends up the process. If you are running your scripts with a batch file in OSGeo4W64 you can do the following:

1) Write this at the end of your script

def Delete():
    with open(workspace+'Delete.py',"w") as wrfile:
        wrfile.write('import shutil\n')

This will create a new python file that will delete this specific folder.

2) In the batch file, you have both your usual file and the name of the new one that is going to delete the folder. The batch file ends up as follows:

python3 D:/.../UsualScript.py


python3 D:/.../Delete.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.