Bug report #13977

QgsVectorFileWriter: bug at saving vector layer to shapefile

Added by Hannes Kohlmann almost 9 years ago. Updated almost 9 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:PyQGIS Console
Affected QGIS version:master Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:21991

Description

When trying to save a layer, I get error number 3 in the case of the following example:
QgsVectorFileWriter.writeAsVectorFormat(layer, "C:\\Users\\Myname\\Desktop\\test1\\test2\\vectorlayer.shp", "utf-8", None, "ESRI Shapefile") would NOT work, while
QgsVectorFileWriter.writeAsVectorFormat(layer, "C:\\Users\\Myname\\desktop\\Test1\\Test2\\vectorlayer.shp", "utf-8", None, "ESRI Shapefile") does work (focus on the upper/lower case of the filepath)!

There seems to be case sensitivity after my folder "desktop"..lower case of "desktop" itself does not matter but if any of the folders after desktop is lower case, I get without exception the error number 3 from QgsVectorFileWriter::WriterError

History

#1 Updated by Hannes Kohlmann almost 9 years ago

Platform used while detecting this bug was Windows 7. I don't know know about Linux, where case sensitivity of paths is native!

#2 Updated by Simon Chenery almost 9 years ago

Use one of the following filenames to avoid Python escape sequence conversion of \\t to TAB in your filename:

"C:\\\\Users\\\\Myname\\\\Desktop\\\\test1\\\\test2\\\\vectorlayer.shp"
"C:/Users/Myname/Desktop/test1/test2/vectorlayer.shp"

Does that fix your problem?

#3 Updated by Giovanni Manghi almost 9 years ago

  • Target version deleted (Future Release - High Priority)
  • Status changed from Open to Feedback

#4 Updated by Sebastian Dietrich almost 9 years ago

Simon Chenery wrote:

Use one of the following filenames to avoid Python escape sequence conversion of \\t to TAB in your filename:

"C:\\\\Users\\\\Myname\\\\Desktop\\\\test1\\\\test2\\\\vectorlayer.shp"
"C:/Users/Myname/Desktop/test1/test2/vectorlayer.shp"

Does that fix your problem?

I could reproduce using the OP's setup. And for me, using either one of your solutions fixes it.

Too bad Microsoft chose the wrong slash as their directory separator :-(

>>> QgsVectorFileWriter.writeAsVectorFormat(layer, "C:\\Users\\Myname\\Desktop\\test\\vectorlayer.shp", "utf-8", None, "ESRI Shapefile")
3
>>> QgsVectorFileWriter.writeAsVectorFormat(layer, "C:\\Users\\Myname\\Desktop\\Test\\Vectorlayer.shp", "utf-8", None, "ESRI Shapefile")
0

QgsVectorFileWriter.writeAsVectorFormat(layer, "C:\\Users\\Myname\\Desktop\\\\test\\Vectorlayer.shp", "utf-8", None, "ESRI Shapefile")
0
QgsVectorFileWriter.writeAsVectorFormat(layer, "C:\\Users\\Myname\\Desktop/test\\Vectorlayer.shp", "utf-8", None, "ESRI Shapefile")
0

And yes, one should escape every \\, not just the one causing problems. I plead guilty of laziness :-)

#5 Updated by Nyall Dawson almost 9 years ago

  • Status changed from Feedback to Closed
  • Resolution set to invalid

As explained, not a bug in QGIS

Also available in: Atom PDF