I am trying to use part of a script to add points from a delimited text file, however, when I use it the layers are not loaded on the screen and not saved in the path indicated as .shp
import os
import qgis.core
indir = '/home/leo/Área de Trabalho/sle/'
outdir = '/home/leo/Área de Trabalho/sle/'
for root, dirs, files in os.walk(indir):
for file in files:
if file.endswith('.txt'):
fullname = os.path.join(root, file).replace('\\', '/')
filename = os.path.splitext(os.path.basename(fullname))[0]
uri = 'file:///%s?crs=%s&delimiter=%s&xField=%s&yField=%s&decimal=%s' % (fullname, 'EPSG:4326', ',', 'field2', 'field1', '.')
layer = QgsVectorLayer(uri, 'pontos_' + str(filename), 'delimitedtext')
QgsVectorFileWriter.writeAsVectorFormat(layer, "pontos_" + str(file) + ".shp", "utf-8", layer.crs(), "ESRI Shapefile")
QgsProject.instance().addMapLayer(layer)
when I do:
print(layer)
<QgsMapLayer: 'pontos_caminho_partículas_3' (delimitedtext)>
In this case, I want to read several .txt files, with coordinates and add these points as shape in QGIS. I am using QGIS 3.10.