I have created a script to add images into a map. Both arcpy.mp.ArcGISProject('CURRENT') and arcpy.mp.ArcGISProject(project_path) allow me to access the project and print the current layers but using the path does not allow me to add data in. Here is my script:
tif_folder = r"C:\folder"
# Set aprx as your current project
aprx = arcpy.mp.ArcGISProject('CURRENT')
#aprx = arcpy.mp.ArcGISProject('project_path')
# Get the map from the project using the index for a given map
map = aprx.listMaps()[0]
#assign existing layers
existinglayernames = [l.name for l in map.listLayers()]
print(f'existinglayernames: {existinglayernames}')
#add files that are not in the project yet
for file in os.listdir(tif_folder):
fName, fExt = os.path.splitext(file)
if fExt != '.tif' or fName + fExt in existinglayernames:
# Skip this file and continue to the next one
continue
filepath = os.path.join(tif_folder, file)
map.addDataFromPath(filepath)
print(f'File name: {fName}')
Both options print the existing layers and the layers that are being added. but, the path does not actually add the file.