I'm using QGIS v3.26, and cannot seem to get code referenced from another Q&A to work. This is the previous Q&A on this topic that I was looking at: How to create multiple QGIS layers from multiple text files?
The files I'm attempting to import are from the EDGAR database showing spatial emission measurements in .txt files. They have three headers; when I manually import a layer using the gui in QGIS I have to select "Number of header lines to discard: 2" under the Record and Field Options tab. The third header has the column names.
import os.path, glob
from qgis.core import QgsProject, QgsVectorLayer
layers=[]
for file in glob.glob('/home/Documents/*.txt'): # Change this base path
uri = "file:///" + file + "?delimiter=%s&xField=%s&yField=%s&useHeader=yes&crs=epsg:4326" % (";", "lat","lon")
vlayer = QgsVectorLayer(uri, os.path.basename(file), "delimitedtext")
vlayer.setFieldAlias(0,'Y')
vlayer.setFieldAlias(1,'X')
layers.append(vlayer)
QgsProject.instance().addMapLayers(layers)
If I run this code all I get is "SyntaxError: invalid syntax"