Hello everyone,
i try to remove a layer that located in D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp
with this code but i get an error:
import arcpy,os,sys import arcpy.mapping from arcpy import env env.workspace = r"C:\Project" counter = 0 for mxdname in arcpy.ListFiles("*.mxd"): print mxdname # print list of mxd's in the folder mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname) df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] for lyr in arcpy.mapping.ListLayers(mxd, "", df): if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp": arcpy.mapping.RemoveLayer(df, lyr) print 'remove' mxd.save() del mxd
this is the error:
>>> environment.mxd remove Traceback (most recent call last): File "C:\yaron\shonot\software\gis\tools\YARON_SCRIPTS\RemoveLayer by data source.py", line 15, in <module> if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp": File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\_base.py", line 78, in _get (attr_name, self.__class__.__name__)) NameError: The attribute 'dataSource' is not supported on this instance of Layer. >>>
thanks for any help!!
Solved! Go to Solution.
Your crash is because one of the "Layers" returned from ListLayers() does not support the data source property! As seen in the error messagge :
NameError: The attribute 'dataSource' is not supported on this instance of Layer.
A basic reason for this could be because you have "Group" layers setup in your table of contents... these are classed as layers and returned by the ListLayers() function, these obviously have no "DataSource" property, so would cause the crash you see.
Im not sure of the 'proper way' to catch this, but I think this should work (Not tested sorry!):
try: if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp": arcpy.mapping.RemoveLayer(df, lyr) print 'remove' except: arcpy.AddMessage("Layer skipped as does not support dataSource property, layer name: " + lyr.name)
Or the full revised script:
import arcpy,os,sys import arcpy.mapping from arcpy import env env.workspace = r"C:\Project" counter = 0 for mxdname in arcpy.ListFiles("*.mxd"): print mxdname # print list of mxd's in the folder mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname) df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] for lyr in arcpy.mapping.ListLayers(mxd, "", df): try: if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp": arcpy.mapping.RemoveLayer(df, lyr) print 'remove' except: arcpy.AddMessage("Layer skipped as does not support dataSource property, layer name: " + lyr.name) mxd.save() del mxd
Read this...
http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/removelayer.htm
then this
http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/layer-class.htm
Why not just use
http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/delete.htm
to delete the shapefile since apparently you don't have a lyr file
Your crash is because one of the "Layers" returned from ListLayers() does not support the data source property! As seen in the error messagge :
NameError: The attribute 'dataSource' is not supported on this instance of Layer.
A basic reason for this could be because you have "Group" layers setup in your table of contents... these are classed as layers and returned by the ListLayers() function, these obviously have no "DataSource" property, so would cause the crash you see.
Im not sure of the 'proper way' to catch this, but I think this should work (Not tested sorry!):
try: if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp": arcpy.mapping.RemoveLayer(df, lyr) print 'remove' except: arcpy.AddMessage("Layer skipped as does not support dataSource property, layer name: " + lyr.name)
Or the full revised script:
import arcpy,os,sys import arcpy.mapping from arcpy import env env.workspace = r"C:\Project" counter = 0 for mxdname in arcpy.ListFiles("*.mxd"): print mxdname # print list of mxd's in the folder mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname) df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] for lyr in arcpy.mapping.ListLayers(mxd, "", df): try: if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp": arcpy.mapping.RemoveLayer(df, lyr) print 'remove' except: arcpy.AddMessage("Layer skipped as does not support dataSource property, layer name: " + lyr.name) mxd.save() del mxd
As an alternative to the try/catch approach you could just check if the layer supports datasource.
import arcpy import os import sys from arcpy import mapping as m arcpy.env.workspace = r"C:\Project" counter = 0 for mxdPath in arcpy.ListFiles("*.mxd"): print(mxdPath) mxd = m.MapDocument(mxdPath) df = m.ListDataFrames(mxd, "Layers")[0] for lyr in m.ListLayers(mxd, "", df): if not lyr.supports("DATASOURCE"): continue if lyr.dataSource == r"D:\PROJECTS\zfonGivatShmuel\gis\layers\6_9_15\gvul.shp": m.RemoveLayer(df, lyr) print("Remove") mxd.save() del mxd