4

I am trying to use the 'merge vector layers' tool in PyQGIS to merge all of the layers in my QGIS project into one shapefile, as part of a larger script I am developing. I tried this script, but I get an error related to the input layers. I am using QGIS 3.0.0.

processing.algorithmHelp('native:mergevectorlayers')
input = iface.mapCanvas().layers()  
processing.run('native:mergevectorlayers',{'LAYERS':input,'CRS':4326,'OUTPUT':'C:\Users\hp\Desktop\COOPI\GIS_Data\CWW_Predictive_Targeting\GEOREF_HH_GPS_Data\Shapefiles\Merged\TA_Mponda_GVH_Bolera_malunga.shp'})

This is the error I get:

Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python36\lib\code.py", line 64, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python36\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\OSGEO4~1\apps\Python36\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\OSGEO4~1\apps\Python36\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\OSGEO4~1\apps\Python36\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
5
  • use double slash in yout paths
    – Fran Raga
    Jul 18, 2018 at 6:57
  • Unfortunately I am still getting an error when I use the double slashes..
    – Simba06
    Jul 18, 2018 at 8:22
  • Traceback (most recent call last): File "C:\OSGEO4~1\apps\Python36\lib\code.py", line 91, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\tools\general.py", line 84, in run return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context) File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\core\Processing.py", line 190, in runAlgorithm raise QgsProcessingException(msg) _core.QgsProcessingException: There were errors executing the algorithm.
    – Simba06
    Jul 18, 2018 at 8:43
  • It worked with single forward slashes / in the file paths
    – Simba06
    Jul 18, 2018 at 9:25
  • @J Clifford, please provide that finding as an answer.
    – artwork21
    Jul 20, 2018 at 12:14

1 Answer 1

2

The code works when using a single forward slash (/) in the file paths

1
  • Alternatively, someone can add a r in front of the file path string (just before the first quote ') and continue using forward slash. It's called a raw string. May 4, 2021 at 15:49

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.