removeLayer() error

445
2
12-22-2022 01:30 PM
ZacharyStanley
New Contributor III

Hello-

I'm simply trying to remove a layer from an aprx using arcpy and am getting an error related to the save() function. Below is code and resulting error:

 

 

# import modules
import arcpy
from arcpy import mp
import os

# remove temporary layers from the map
aprx = mp.ArcGISProject(r"N:\Projects\ElectionsOffice\VoterAddresses\VoterAddresses.aprx")
aprxMap = aprx.listMaps()[0]
for lyr in aprxMap.listLayers():
    if lyr.name == 'sgmj_2872_Layer2':
        aprxMap.removeLayer(lyr)
aprx.save()

 

 

OSError                                   Traceback (most recent call last)
In  [40]:
Line 7:     aprx.save()

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in save:
Line 308:   return convertArcObjectToPythonObject(self._arc_object.save(*gp_fixargs((), True)))

OSError: N:\Projects\ElectionsOffice\VoterAddresses\VoterAddresses.aprx

 

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

related information

Solved: removeLayer require some kind of refresh? - Esri Community

I would throw in a print statement or two since I suspect you have no aprxMap or perhaps layers to process.

If all seems fine try a saveAs instead of save, using a different output name to ensure that the process can complete with some form of "save" option


... sort of retired...
0 Kudos
DonMorrison1
Occasional Contributor III

Maybe you (or somebody else) has the project open in ArcGIS Pro?  

0 Kudos