I'm writing a python script tool that will recursively search through a folder and all subfolders and find all .lyr files. Ultimately, I'm writing a CSV file which contains the path of the .lyr file, the name, the source, and the definition query.

So far, so good. I'm able to do this with no troubles.

The tricky bit is to get the source of all sub-layers in a group layer. Because group layers can be nested and sub-nested, etc, I decided to write a recursive function to get the properties of the layers. For the sake of clarity in this question, I've modified the code to just print instead of returning all the properties of the actual function.

Code for the recursive function:

def searchLayer(layerObj):
    if not layerObj.isGroupLayer:
        print layerObj.longName
        for sublayer in layerObj:

Again, this works for the most part. However, it's not printing the longName property as I would expect. Suppose I have a .lyr file called "Administrative Boundaries". And it looks like this:

Administrative Boundaries
    - Province Boundaries
    - Local Government Boundaries
    - National Boundaries

What I would expect to see as an output is the following:

Administrative Boundaries\Province Boundaries 
Administrative Boundaries\Local Government Boundaries 
Administrative Boundaries\National Boundaries

However, what I see instead is:

Province Boundaries
Local Government Boundaries
National Boundaries

As you can see, it's not printing the full longName path as it should. What am I doing wrong here?

I'm using ArcGIS Desktop 10.3.1 - Basic License.

If I run the following code in an MXD, I get the expected results.

mxd = arcpy.mapping.MapDocument('current')
layerList = arcpy.mapping.ListLayers(mxd)
for layer in layerList:
    print layer.longName
  • 1
    I've come across this before. It may be a bug. Similar issue here. Does it print the longName property when you test an mxd that's open?
    – nwduncan
    Commented Feb 23, 2016 at 1:42
  • 1
    Yes. I've edited my question to reflect that. I had seen that post and was hoping it wasn't a bug.
    – Fezter
    Commented Feb 23, 2016 at 1:48

1 Answer 1


It seems as when you use the for sublayer in layerObj: in the searchLayer, you don't get a proper layer object back. I use arcpy.mapping.ListLayers instead.

This is the code that works for me:

import arcpy,os
wkspc = r"C:\GIS\Temp\RecursiveLayers"
for lyr_file in [f for f in os.listdir(wkspc) if f.split(".")[1] == "lyr"]:
    lyr_path = os.path.join(wkspc,lyr_file)
    lyr = arcpy.mapping.Layer(lyr_path)
    print "Lyr file name: " + lyr.name

If I have a .lyr file saved from this .mxd structure:

enter image description here

and having this function:

def searchLayer(layerObj):
    if not layerObj.isGroupLayer:
        print layerObj.longName
        for sublayer in arcpy.mapping.ListLayers(layerObj):
            if not sublayer.isGroupLayer:

Then this is what I get printed:

> Lyr file name: FirstLevelGroupLayer2
> FirstLevelGroupLayer2\SecondLevelGroupLayer2\SecLev_Warehouses2
> FirstLevelGroupLayer2\SecondLevelGroupLayer2\SecLev_Stores2
> FirstLevelGroupLayer2\Warehouses2 
> FirstLevelGroupLayer2\Stores2

The problem with any group layer seems to be that you get even the group layer name when listing the layers in a group layer. So, I had to work around that by adding if not sublayer.isGroupLayer: in the search function.

Your Answer

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

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