Skip to main content
I changed the directory of the Select_folder, because the loop doesn't iterate through the subdirectories.
Source Link

Actually when you reused the "layers" variable as the loop variable, you deleted the content of the first variable that has the same name. Instead, you should use the wildcard as a parameter within the call to the "glob" function.

Try to do the following:

import os
import processing
import glob

Select_folder="C:/*/VA*/*/*/"
Save_results= "C:/*/merged"merged/"
wildcard = "*GST_*.shp" #select only shapefile layers that contain the phrase GST
os.chdir(Select_folder)

layers = glob.glob('**/'Select folder + wildcard, recursive=True)

processing.run("native:mergevectorlayers", {'INPUT''LAYERS':[layers]layers,'CRS':QgsCoordinateReferenceSystem('EPSG:31255'),'OUTPUT':[Save_results+'/'+'GSTSave_results+'GST.shp']shp'})


## load merged GST layer
GST_merged = QgsVectorLayer('C:/*/merged','GST','ogr')
QgsProject.instance().addMapLayer(GST_merged, True)

Actually when you reused the "layers" variable as the loop variable, you deleted the content of the first variable that has the same name. Instead, you should use the wildcard as a parameter within the call to the "glob" function.

Try to do the following:

import os
import processing
import glob

Select_folder="C:/*/"
Save_results= "C:/*/merged"
wildcard = "*GST_*.shp" #select only shapefile layers that contain the phrase GST
os.chdir(Select_folder)

layers = glob.glob('**/' + wildcard, recursive=True)

processing.run("native:mergevectorlayers", {'INPUT':[layers],'CRS':QgsCoordinateReferenceSystem('EPSG:31255'),'OUTPUT':[Save_results+'/'+'GST.shp']})


## load merged GST layer
GST_merged = QgsVectorLayer('C:/*/merged','GST','ogr')
QgsProject.instance().addMapLayer(GST_merged, True)

Actually when you reused the "layers" variable as the loop variable, you deleted the content of the first variable that has the same name. Instead, you should use the wildcard as a parameter within the call to the "glob" function.

Try to do the following:

import os
import processing
import glob

Select_folder="C:/*/VA*/*/*/"
Save_results= "C:/*/merged/"
wildcard = "*GST_*.shp" #select only shapefile layers that contain the phrase GST
os.chdir(Select_folder)

layers = glob.glob(Select folder + wildcard)

processing.run("native:mergevectorlayers", {'LAYERS':layers,'CRS':QgsCoordinateReferenceSystem('EPSG:31255'),'OUTPUT':Save_results+'GST.shp'})


## load merged GST layer
GST_merged = QgsVectorLayer('C:/*/merged','GST','ogr')
QgsProject.instance().addMapLayer(GST_merged, True)
Modified the syntax
Source Link

Actually when you reused the "layers" variable as the loop variable, you deleted the content of the first variable that has the same name. Instead, you should use the wildcard as a parameter within the call to the "glob" function.

Try to do the following:

import os
import processing
import glob

Select_folder="C:/*/"
Save_results= "C:/*/merged"
wildcard = "*GST_*.shp" #select only shapefile layers that contain the phrase GST
os.chdir(Select_folder)

layers = glob.glob(select_folder,'**/' + wildcard, recursive=True)

processing.run("native:mergevectorlayers", {'INPUT':[layers],'CRS':QgsCoordinateReferenceSystem('EPSG:31255'),'OUTPUT':[Save_results+'/'+'GST.shp']})


## load merged GST layer
GST_merged = QgsVectorLayer('C:/*/merged','GST','ogr')
QgsProject.instance().addMapLayer(GST_merged, True)

Actually when you reused the "layers" variable as the loop variable, you deleted the content of the first variable that has the same name. Instead, you should use the wildcard as a parameter within the call to the "glob" function.

Try to do the following:

import os
import processing
import glob

Select_folder="C:/*/"
Save_results= "C:/*/merged"
wildcard = "*GST_*.shp" #select only shapefile layers that contain the phrase GST
os.chdir(Select_folder)

layers = glob.glob(select_folder, wildcard)

processing.run("native:mergevectorlayers", {'INPUT':[layers],'CRS':QgsCoordinateReferenceSystem('EPSG:31255'),'OUTPUT':[Save_results+'/'+'GST.shp']})


## load merged GST layer
GST_merged = QgsVectorLayer('C:/*/merged','GST','ogr')
QgsProject.instance().addMapLayer(GST_merged, True)

Actually when you reused the "layers" variable as the loop variable, you deleted the content of the first variable that has the same name. Instead, you should use the wildcard as a parameter within the call to the "glob" function.

Try to do the following:

import os
import processing
import glob

Select_folder="C:/*/"
Save_results= "C:/*/merged"
wildcard = "*GST_*.shp" #select only shapefile layers that contain the phrase GST
os.chdir(Select_folder)

layers = glob.glob('**/' + wildcard, recursive=True)

processing.run("native:mergevectorlayers", {'INPUT':[layers],'CRS':QgsCoordinateReferenceSystem('EPSG:31255'),'OUTPUT':[Save_results+'/'+'GST.shp']})


## load merged GST layer
GST_merged = QgsVectorLayer('C:/*/merged','GST','ogr')
QgsProject.instance().addMapLayer(GST_merged, True)
Source Link

Actually when you reused the "layers" variable as the loop variable, you deleted the content of the first variable that has the same name. Instead, you should use the wildcard as a parameter within the call to the "glob" function.

Try to do the following:

import os
import processing
import glob

Select_folder="C:/*/"
Save_results= "C:/*/merged"
wildcard = "*GST_*.shp" #select only shapefile layers that contain the phrase GST
os.chdir(Select_folder)

layers = glob.glob(select_folder, wildcard)

processing.run("native:mergevectorlayers", {'INPUT':[layers],'CRS':QgsCoordinateReferenceSystem('EPSG:31255'),'OUTPUT':[Save_results+'/'+'GST.shp']})


## load merged GST layer
GST_merged = QgsVectorLayer('C:/*/merged','GST','ogr')
QgsProject.instance().addMapLayer(GST_merged, True)