8

I´m using python in QGIS 2.2 and struggling with an error trying this:

# create layer from text
_vlayer = QgsVectorLayer(_ur1, "raw", "delimitedtext")

works fine but:

# export layer to shape
_writer = QgsVectorFileWriter.writeAsVectorFormat\
(_vlayer,"hoppla.shp","utf-8",None,"ESRI Shapefile")

... ends up wiht error code "3" - according to documentation:"ErrCreateLayer".

0

2 Answers 2

5

It's all about user rights.

Just changed to a directory I'm allowed to write, everything works fine.

_writer = QgsVectorFileWriter.writeAsVectorFormat(_vlayer,r"C:/Users/myName/xx/hoppla.shp","utf-8",None,"ESRI Shapefile")
0

I had the same problem and figured out, that there seems to be a bug in the class QgsVectorFileWriter.writeAsVectorFormat

I was pretty sure that I had permission for the folder and in my case this did NOT work anyway: QgsVectorFileWriter.writeAsVectorFormat\ (layer,"C:\Users\Myname\Desktop\test\hoppla.shp","utf-8",None,"ESRI Shapefile") although the folder 'test' existed on my desktop and had all rights.

Now the most absurd thing to try was to make "test" upper case --> "Test" QgsVectorFileWriter.writeAsVectorFormat\ (layer,"C:\Users\Myname\Desktop\Test\hoppla.shp","utf-8",None,"ESRI Shapefile") and now it worked! Further I tried to check the folders before the last folder where the file was to be saved in for case sensitivity and if I changed (in this code example) 'Desktop' to lower case ('desktop') it worked!

On the other hand something like QgsVectorFileWriter.writeAsVectorFormat(temp_layer, "C:\Users\Myname\desktop\test1\test2\wasneues4.shp", "utf-8", None, "ESRI Shapefile") would NOT work, but QgsVectorFileWriter.writeAsVectorFormat(temp_layer, "C:\Users\Myname\desktop\Test1\Test2\wasneues4.shp", "utf-8", None, "ESRI Shapefile") with both T's upper case works! Very weird...

Hopefully this was not driving too many people crazy ^^ I will report this bug.

My QGis Version: 2.8.2, so not the latest.

3
  • But the problem still exists in the 2.12.1 version ;)
    – khannes
    Dec 11, 2015 at 10:16
  • 1
    Just saw this: use RAW strings or Python will escape your characters: \t is a tab, which is obviously not allowed in filenames... Not a bug. r"C:\Users\Myname\Desktop\Test\hoppla.shp" sure works as expected..
    – nilsnolde
    Dec 23, 2018 at 21:40
  • As @nnolde said, you cannot use \t or \n, because they are reserved as tab and new line, respectively. The best practice to use the forward slash to selarate folders, i.e., "C:/users/myname/desktop/test/hoopla.shp" or to add the raw processing character r before the string quotations. The former is compatible with windows, mac, and linux. May 17 at 4:07

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.