list layers in maps

481
3
05-22-2014 12:08 PM
stansovern
New Contributor
Hi Folks,
I'm trying to generate a list of maps, and the layers in those maps in python. This script works once, then the line that brings up the path desc = arcpy.Describe(map) will not return a path.  Any ideas?

import arcpy
from arcpy import env


# Loop through all MXDs in the specified folder and list layers in each MXD
folderPath = r"mydata"
env.workspace = folderPath
maps = arcpy.ListFiles("*.mxd")       # makes a list of maps in folderPath
for map in maps:
[INDENT]desc = arcpy.Describe(map)        # make a describe object
mappath = desc.path + '\\'+ desc.file  #build a path to the map
print mappath
lyrlist = arcpy.mapping.ListLayers(mxd)
for lyr in lyrlist:
[INDENT]print lyr.dataSource[/INDENT][/INDENT]
0 Kudos
3 Replies
JasonScheirer
Occasional Contributor III
I'm surprised this works at all for you. Describe is not the appropriate function to use here.

Try this:

import arcpy
from arcpy import env

# Loop through all MXDs in the specified folder and list layers in each MXD
folderPath = r"mydata"
env.workspace = folderPath
maps = arcpy.ListFiles("*.mxd") # makes a list of maps in folderPath
for map in maps:
    mxd_path = os.path.join(env.workspace, map)
    print mxd_path
    mxd = arcpy.mapping.MapDocument(mxd)
    lyrlist = arcpy.mapping.ListLayers(mxd)
    for lyr in lyrlist:
        print lyr.dataSource
0 Kudos
IanMurray
Frequent Contributor
looking at your code

lyrlist = arcpy.mapping.ListLayers(mxd)

should be

lyrlist = arcpy.mapping.ListLayers(map)

you never create a variable called mxd, you'd probably need to substitute it with your local variable map, as it refer to the map in your list of map files.
0 Kudos
stansovern
New Contributor
Awesome Jason, thanks!
0 Kudos