Expanding on my previous question Remove Layers ,
i try to remove layers from mxd's (in ArcMap 10.3, Python 2.7.8), that don't appear in the data frame.
The layers located in different disk drivers and in different folders and sub folders and each layer has different path orientation (right to left- different language from English). Only the MXD's located in "D:\desktop\Project" I using this code:
# -*- coding: cp1255 -*- import arcpy,os from arcpy import env env.workspace = r"D:\desktop\Project" for mxdname in arcpy.ListFiles('*.mxd'): print mxdname mxd = arcpy.mapping.MapDocument(os.path.join(env.workspace, mxdname)) df = arcpy.mapping.ListDataFrames(mxd, "Layers") for lyr in arcpy.mapping.ListLayers(mxd, "" ,df): print str(lyr.getExtent()) print lyr.name if lyr.getExtent() != None: if df.extent.disjoint(lyr.getExtent()): arcpy.mapping.RemoveLayer(df, lyr) print u'Removed '+unicode(lyr) + ' lyr' mxd.save() del mxd