I have written a code that imports the different Layers from a folder to QGIS. The problem is that when I run the code again, the layers are duplicated and so on. If I try to remove the layer at the beginning of the code with:
The first time I run the code, I wont work since the layers do not exist yet. To check with layers are in the project, I used this command:
for layer in QgsProject.instance().mapLayers().values(): print(layer.name())
With this I become a list with the following existing layers:
ne_50m_coastline ne_50m_graticules_30 ne_50m_ocean ne_50m_rivers_lake_centerlines
Is there a way to remove the layers only if they already exist in the project?