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.

input = iface.mapCanvas().layers()  

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
  • use double slash in yout paths
    – Fran Raga
    Commented Jul 18, 2018 at 6:57
  • Unfortunately I am still getting an error when I use the double slashes..
    – Simba06
    Commented 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
    Commented Jul 18, 2018 at 8:43
  • It worked with single forward slashes / in the file paths
    – Simba06
    Commented Jul 18, 2018 at 9:25
  • @J Clifford, please provide that finding as an answer.
    – artwork21
    Commented Jul 20, 2018 at 12:14

1 Answer 1


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

  • 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. Commented May 4, 2021 at 15:49

Your Answer

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

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