Quick workaround for PyQGIS error “file is not a directory” when saving files
Fellow researchers and open-source GIS enthusiasts,
Welcome to my blog!
I’d like to start with a disclaimer – I may be a researcher of this very area but that doesn’t mean everything I do or write here will work for you, in your own desktop configurations and package versions. I have no responsibility if you lose data or mess up your installation. I also do not authorize any copies of my content.
Today, I will explore another PyQGIS (Python Console of QGIS) Error and its causes. Unfortunately, I didn’t find a simple solution for this one. I did, however, find a workaround.
Description of the error
This error is especially common when you are re-running a code you have already ran in the past. Sometimes, as I am coding, I run an incomplete version of my code, just to know if the compiler/interpreter accuses any errors. The codes frequently include open and saving items. I thought that the mentioned file would be overwritten every time I ran a file saving in the QGIS Python Console. Instead, I received this error:
_core.QgsProcessingException: Could not create layer folder/output.shp: Creation of data source failed (OGR error: folder/output.shp is not a directory.)
I went there and checked, and, in fact, output.shp is not a directory. It is the file I want to overwrite but is not recognized as a file to be overwritten.
What is the code I was trying to run when I got the error?
I got the error when trying to run a code similar to the one shown below, in a loop inside the Python Console. My original idea was for the file to be overwritten at every loop.
paramexp= {'INPUT': extent,'OUTPUT': 'folder/output.shp'}
processing.run("native:extenttolayer", paramexp)
The workaround
Case 1: The file saving that triggers this error is not inside a loop
In this case, simply deleting the file before every time re-running the code will probably solve the issue. If it doesn’t, try closing QGIS and re-opening it. Sometimes, when the code is stopped midway, it leaves open files, which prevents the code to re-run correctly.
Case 2: The file saving that triggers this error is inside a loop
In this case, I saw no other alternative than creating a counter inside the loop and adding it to the name of the file, in order not to have two identical filenames. Other than a counter, it can be done with the enumerate command as well, or, if that’s the case, or you can use any variable name that is unique for a given iteration. An example:
counter=0
for i in list:
counter=counter+1
outputname='folder/filename'+str(counter)+'.shp' #name of the file
paramexp= {'INPUT': extent_given,'OUTPUT':outputname} #parameters
processing.run("native:extenttolayer", paramexp) #processing and saving the file
you can later use Python itself or any other facility to delete the generated files automatically. On Ubuntu, I would simply open a new Terminal and type
rm folder/filename*.shp
after the execution of the Python code.
If the files are too large and you’ll run out of space before the Python loop ends, just add a line in your Python code in which you delete the file generated on the previous iteration. Something like:
import os
counter=0
for i in list:
os.remove(outputname)
counter=counter+1
outputname='folder/filename'+str(counter)+'.shp' #name of the file
paramexp= {'INPUT': extent_given,'OUTPUT':outputname} #parameters
processing.run("native:extenttolayer", paramexp) #processing and saving the file
I hope this post is helpful to you, and, in case you find a more direct solution for this issue, please let me know!