4

What the code does successfully so far, is gets a list of the layers in the first data frame of the current map. What it doesn't succeed in doing is, joining the layer names to the gdb location using os.path.join(), so I can then do a clip against an existing feature class.

I think I need to convert the lyr names to a string before I can use it in a Clip_analysis, how do I do that?

Here is what I've got so far:

import arcpy
import arcpy.mapping
import os

#Set the current map
mxd = arcpy.mapping.MapDocument("CURRENT")

# Set the dataframe
df = arcpy.mapping.ListDataFrames(mxd)[0]

# Get list of Layers from toc
layers = arcpy.mapping.ListLayers(mxd,"",df)

#Set the output workspace
outWorkspace = r'C:\Users\jsommerville\Documents\ArcGIS\California\ProjectData.gdb'

for lyr in layers: 
    outFeatureClass = os.path.join(outWorkspace, lyr) 
    arcpy.Clip_analysis(lyr, "extent", outFeatureClass)

The error message I'm getting: 'Layer' object is not subscriptable

1 Answer 1

6

You are correct about needing to convert the layer name to a string. You can get the layer name as a string by calling lyr.name. The reason for this is each layer is a layer object, which won't work in os.path.join(). However, the object has a 'name' property that you can set or pass to other string functions. To use it, call os.path.join(outWorkspace,lyr.name).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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