I am attempting to list all of the shapefiles in the "Layers" dataframe of my Table of Contents. I've also created a list of layer files (.lyr) in a specified folder. My goal is to assign a layer file in this folder to a shapefile in my TOC based on common characters betweenthe two. I've set up both of these lists in my script. One block of code generates and prints the TOC shapefiles, another block generates the layer files in the specified folder.For example:An entry in layer file list looks like this:N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles\031g04_9_0_LX_2420009_1.lyrAn entry in a TOC shapefile list looks like this:031g04_9_0_LX_2420009_1This I've managed to do without error.What I want to do next is take each shp in the TOC and match it to it's corresponding layer file in the folder.I was hoping to do that by comparing a subsection of each element in each list.For example, I would extract from the layer the "_9_0_LX_2420009_1" [59:-4] text string. If it equals same subsection of the shp TOC list, "_9_0_LX_2420009_1" [6:], I would apply the corresponding layer file to the shapefile. My code is pasted below.# Author: Zachary Bartlett
# February 15, 2012
# Location: N:\DOCUMENTATION\PYTHON\Canvec_Symbolization\canvec_autosym.py
# This tool searches the ArcMap TOC for Canvec shapefiles and symbolizes them
# based on layer files in:
# N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles
import arcpy, glob, os
arcpy.env.Workspace = r"N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles"
arcpy.env.OverwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layer_file_list = glob.glob(str(arcpy.env.Workspace) + "\*.lyr")
arcpy.AddMessage("Layer File List: ")
for layer in layer_file_list:
arcpy.AddMessage(layer)
print layer_file_list
arcpy.AddMessage("-------")
TOC_shp_list = arcpy.mapping.ListLayers(mxd, "", dataFrame)
arcpy.AddMessage("Table of Contents Shapefile list: ")
for shp in TOC_shp_list:
arcpy.AddMessage(shp)
print TOC_shp_list
arcpy.AddMessage("-------")
for shp in TOC_shp_list:
shp_ext = shp[6:]
for lyr in layer_file_list:
lyr_ext = lyr[59:-4]
print lyr_ext
if lyr_ext == shp_ext:
ApplySymbologyFromLayer_management(shp, lyr)
del lyr, lyr_ext
del shp, shp_ext
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
When I run this script I get the following error:<type 'exceptions.TypeError'>: 'Layer' object is unsubscriptableFailed to execute (canvecautosym).I even tried removing some of the problematic code and running the following (I simplified the for loop):# Author: Zachary Bartlett
# February 15, 2012
# Location: N:\DOCUMENTATION\PYTHON\Canvec_Symbolization\canvec_autosym.py
# This tool searches the ArcMap TOC for Canvec shapefiles and symbolizes them
# based on layer files in:
# N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles
import arcpy, glob, os
arcpy.env.Workspace = r"N:\BASE_DATA\CANVEC\Style_Guides\Canvec_Layer_Styles"
arcpy.env.OverwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layer_file_list = glob.glob(str(arcpy.env.Workspace) + "\*.lyr")
arcpy.AddMessage("Layer File List: ")
for layer in layer_file_list:
arcpy.AddMessage(layer)
print layer_file_list
arcpy.AddMessage("-------")
TOC_shp_list = arcpy.mapping.ListLayers(mxd, "", dataFrame)
arcpy.AddMessage("Table of Contents Shapefile list: ")
for shp in TOC_shp_list:
arcpy.AddMessage(shp)
print TOC_shp_list
arcpy.AddMessage("-------")
for shp in TOC_shp_list:
shp_ext = shp[6:]
arcpy.AddMessage(shp_ext)
print shp_ext
del shp, shp_ext
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
When I run this script I get the same error as before.Everything up to the first for loop runs OK.Any ideas?