2

I work with this FGB annotation (ArcView 10.3):

enter image description here enter image description here

and i try to remove it with this code:

import arcpy,os
from arcpy import env

env.workspace = r"G:\PROJECTS\road611\gis\aa"
for mxdname in arcpy.ListFiles("*.mxd"):
   print mxdname 
   fullPath = env.workspace + "\\" + mxdname
   if 'env_70-42' not in mxdname:
      mxd = arcpy.mapping.MapDocument(fullPath)
      df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
      for lyr in arcpy.mapping.ListLayers(mxd, "*",df):
         if lyr.isGroupLayer == True: continue
         if lyr.dataSource == r"G:\PROJECTS\road611\gis\lyr\18_8_16\New File Geodatabase.gdb\anno":
            arcpy.mapping.RemoveLayer(df, lyr)
            print 'RemoveLayer'
      mxd.save()
      counter = counter + 1  
del mxd

but i failed to remove the layer and i get this error:

NameError: The attribute 'dataSource' is not supported on this instance of Layer

but if i remove manually the "anno" feature class, and try to remove other FGD feature class, then i succeed and get this:

>>> 
antiquities_70-42.mxd
RemoveLayer
1
5

It is better to ask if the layer supports dataSource, then set it. There are lots of layer types and only a few support the dataSource property.

Your code is just skipping group layers.

Try something like:

for lyr in arcpy.mapping.ListLayers(mxd, "*",df):
  if lyr.supports("DATASOURCE"):
    if lyr.dataSource == r"G:\PROJECTS\road611\gis\lyr\18_8_16\New File Geodatabase.gdb\anno":
      arcpy.mapping.RemoveLayer(df, lyr)
      print 'RemoveLayer'
mxd.save()

See the documentation for the Supports method for more details.

If you are trying to access the dataSource for the annotation, see this answer to Using arcpy.Describe Method on Annotation layers?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.