1

I'm batch dissolving some layers in QGIS and want to save each dissolved shapefile as <layername>_dissolved.shp. I'm using a for-loop but it just shows raise QgsProcessingException(msg) _core.QgsProcessingException: There were errors executing the algorithm. error.

Here's my code:

import processing
layers = QgsProject.instance().mapLayers().values()
for i in layers:
    processing.run("native:dissolve", 
                   {'INPUT': i,
                    'OUTPUT': r'C:\Users\imper\Desktop\\' + str(i) + '_dissolved' + '.shp'})
    iface.addVectorLayer(str(i+_dissolved), '', 'ogr')

How should I revise my 'OUTPUT' file path?

  • 1
    Try os.path.join(r'C:\Users\imper\Desktop', '{0}_dissolved.shp'.format(i)) – BERA Nov 22 at 10:54
4

Use double blackslash (\) or slash(/) to separate directories in the path. Your loop variable "i" is a layer object, not the path to the layer, to get the path to the layer use:

for layer in QgsProject.instance().mapLayers().values():
    layer_path = layer.dataProvider().dataSourceUri().split('|')[0]
    output_path = 'C:\\Users\\imper\\Desktop\\' + layer.name() + '_dissolved' + '.shp'
    processing.run("native:dissolve", 
                   {'FIELD': [], 'INPUT': layer_path, 'OUTPUT': output_path})
    iface.addVectorLayer(output_path, layer.name() + '_dissolved', 'ogr')
| improve this answer | |

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.