3

I want to loop through folders and find shapefiles which contains the name "GST" and merging them together using python in QGIS 3.4.3.

In detail: The folder x contains four folders called VA72, VA73, VA75 and VA76. In each of this folders where a lot other folders (72001, 72002, a.s.o.). In each of them are three other folders, one of them is called "SHP". And in this folder there are several shapefiles. One of the shapefiles is called 72001GST_V2.shp. In every folder (VA72, VA73, VA75 and VA76) the structure is the same.

I want to select all shapefile layers which contain the phrase "**GST_*.shp" and merge them together.

I tried a code I've found after modified it, but after performing it nothing happens.

The code is as follows:

import os
import processing
import glob

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

# loop through folders and merge them together
for layers in glob.glob( Select_folder ):
    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)

What is wrong? Has anybody an idea?

1 Answer 1

3

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)
3
  • Thanks for your advice. Unfortunately the code doesn't work. An error occur: "TypeError: glob() takes 1 positional argument but 2 were given". I even tried it with "glob.glob(Select_folder + wildcard)" . The error message "Incorrect parameter value for LAYERS" occured.
    – Helga F.
    Commented Jan 17, 2019 at 10:53
  • I modified the code. This should work now. Commented Jan 17, 2019 at 13:26
  • Thank you for your help. I modified it a little bit, because the loop doesn't iterate throug the subdirectories. But in the end it works the way you suggested. I modified the code in your answer. Best regards
    – Helga F.
    Commented Jan 22, 2019 at 8:36

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.