I am working with a notebook in ArcGIS Pro and I want to add all .lyrx files within a local folder to a map.
I have set the workspace and created a list with:
arcpy.env.workspace = "path to my data"
list_layers = arcpy.ListFiles("*.lyrx")
which works fine. I have tried to create a for loop to iterate though the list and add the layers to the map in my current project without success.
I realise this is probably easy to do but I can't work it out. Any help would be greatly appreciated!
Solved! Go to Solution.
You can see the the two produces the same results (change to your path):
import os
import arcpy
import glob
arcpy.env.workspace = r"C:\Users\...\Documents"
# Copy each file with a .csv extension to a dBASE file
for lyrx_file in arcpy.ListFiles("*.lyrx"):
pth = os.path.join(arcpy.env.workspace, lyrx_file)
print(f'List Files: {pth}')
for filename in glob.iglob(f'{arcpy.env.workspace}/*.lyrx'):
print(f'glob: {filename}')
I can't get either method to work unless I set the LayerFIle first, then it is working as I expect (and I have to append the path as suggested above).
This is working for me:
import os
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Map")[0]
arcpy.env.workspace = r"C:\path\to\lyrfiles"
list_layers = arcpy.ListFiles("*.lyrx")
for layer in list_layers:
insertLyr = arcpy.mp.LayerFile(arcpy.env.workspace + os.sep + layer)
aprxMap.addLayer(insertLyr)
R_
Thanks