I have a file from a file geodatabase, which contains all raster files, that I want to save to an already existing layer file. I am using ArcGIS Pro.
I am trying this like so:
import arcpy
import os
arcpy.env.overwriteOutput = True
#read in the file geodatabase
dnbr = "X:/user/pre_85/dnbr.gdb"
#save all raster names
arcpy.env.workspace = dnbr
all_rasters = arcpy.ListRasters() #this returns ['test', 'test2']
#get the first raster in the list
test = all_rasters[0]
#read in the lyr file
lyr = arcpy.mp.LayerFile("X:/user/arcpy_maps/New_Group_Layer.lyrx")
#list the existing layers in the group layer file
lyr.listLayers() #this returns [<arcpy._mp.Layer at 0x91f3e9c8d0>, <arcpy._mp.Layer at 0x91f3e9c7f0>]
#now lets add the test layer to the third location of lyr
for_adding = os.path.join(dnbr, test)
lyr.addLayerToGroup(for_adding, [2]) #add to the third location, since two exist
but this returns the following value error:
File "<ipython-input-15-07b8bd305e3c>", line 1, in <module>
lyr.addLayerToGroup(one, [2])
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py", line 191, in fn_
return fn(*args, **kw)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 739, in addLayerToGroup
return convertArcObjectToPythonObject(self._arc_object.addLayerToGroup(*gp_fixargs((target_group_layer, add_layer_or_layerfile, add_position), True)))
ValueError: X:/user/pre_85/dnbr.gdb\test