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
else:
for sublayer in layerObj:
searchLayer(sublayer)
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