I have a folder of ~120 csv files.
I would like to add them all to my QGIS project.
I know this is accomplished with the 'Data Source Manager' and 'Delimited Text' option. However, instead of adding them one by one I would like a script to run in the Python console to add every one of them in the folder.
I have started the potential frame for this code:
import os
#Define the source folder
src_folder = 'C:\Users\Rollins\Documents\PVQ_Stuff\General_Election'
#Iterate through the files in the folder
for file in os.scandir(src_folder):
if file.name.endswith('.csv'):
#Define the csv name when imported as a layer
layer_name = file.name[-6:-1]
#This part I made up! I have no idea if any of this syntax is correct, but it's my
# best guess for calling the 'delimited text' tool multiple times and defining the
# parameters based on the current file in the folder
params = {'FILE_NAME':file.name,'LAYER_NAME':layer_name,
'ENCODING':'latin9','FILE_FORMAT':custom_delimiters[semicolon],
'GEOMETRY_DEFINITION':no_geometry}
processing.run("qgis:datasourcemanager:delimitedtext", params)
As I write in the above code, I don't know how to select the 'delimited text' tool and input it's parameters using a script.
glob.glob()
, and you're done.