AnsweredAssumed Answered

RemoveLayer - arcpy

Question asked by yaron148 on Sep 24, 2015
Latest reply on Sep 24, 2015 by FGibson-esristaff

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!!

Outcomes