4

I can't seem to find anything as far as defining a list of layer names and then removing those layers from all mxds within a folder. This is the code I have worked up so far, but I have to cut and paste a new entry for each new layer name. Just getting into programming. I poached a lot of this and made it work for what I needed.

ArcGIS 10.3.1

Windows 10


This is my final product following everyone's advice.

#import modules
import os, arcpy

#provide project folder path
folderPath = "C:\Project Folder"
#define list of layers to be removed from mxd files as the name appears in table of contents
LayersToRemove = ['layer 1','layer 2']
for fileName in [x for x in os.listdir(folderPath) if os.path.splitext(x)[1] == ".mxd"]:
    fullPath = os.path.join(folderPath, fileName)
    if os.path.isfile(fullPath):
        mxd = arcpy.mapping.MapDocument(fullPath)
        print(fileName)
        for df in arcpy.mapping.ListDataFrames(mxd):
            for lyr in arcpy.mapping.ListLayers(mxd, "", df):
                if lyr.name in LayersToRemove:
                    arcpy.mapping.RemoveLayer(df, lyr)
                    print("     " + df.name + ": removed: " + lyr.name)
        #saves a copy of the map
        mxd.saveACopy("copy_" + fileName)
        del mxd
  • 2
    Perhaps define your list of names above e.g. layersToRemove = ['Layer 1', 'Layer 2', 'Layer 3'] and then change the if lyr.name == 'Layer x': to if lyr.name in layersToRemove: – smiller May 1 '18 at 18:00
  • @smiller, you should probably put that in an answer as it is correct. – isamson May 1 '18 at 18:09
  • 1
    Be careful you don't have duplicate layer names. – Vince May 2 '18 at 3:41
  • Your folderPath isn't valid, since the backslashes are not escaped or overridden by raw formatting. – Vince May 2 '18 at 13:15
  • @Vince good point – miketelevision May 2 '18 at 19:25
3

You can define your list of names above e.g. layersToRemove = ['Layer 1', 'Layer 2', 'Layer 3'] and then change the if lyr.name == 'Layer x': to if lyr.name in layersToRemove:

1

here is some code I wrote a few months ago that will crawl through network drives and open up mxds to remove certain layers you want.

import arcpy,os,fnmatch,sys

def find(pattern, path=os.getcwd()):
    for path, dirs, files in os.walk(path):
        for filename in files:
            if fnmatch.fnmatch(filename, pattern): yield os.path.abspath(os.path.join(path, filename))


for f in find('*.mxd',r'path'):
    mxd=arcpy.mapping.MapDocument(r'{}'.format(f))
    print mxd
    for df in arcpy.mapping.ListDataFrames(mxd):
        for lyr in arcpy.mapping.ListLayers(mxd,data_frame=df):
            print lyr
            if lyr.name in("layer1","layer2"):
                arcpy.mapping.RemoveLayer(df,lyr)
    mxd.save()

just replace 'path' with your path and repalce "layer1","layer2" with the layers you want to remove.

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.