I have a folder containing multiple .mxd files. I wish to list all of the layer names and data sources for each .mxd from the folder. I am using the following code which lists the above for one .mxd but how do I loop the code to work through all .mxds in the folder?

import arcpy

mxd = arcpy.mapping.MapDocument("D:\MXDs\Air_quality.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
        print " MIS Name: " + lyr.name + "  Data Source: " + lyr.dataSource

1 Answer 1

>>> import arcpy,os
>>> dp=r'D:\MXDs'
>>> arcpy.env.workspace=r'D:\MXDs'
>>> mxds=arcpy.ListFiles("*.mxd")
>>> for mxd in mxds:
...     fp=os.path.join(dp,str(mxd))
...     md=arcpy.mapping.MapDocument(fp)
...     layerlist=arcpy.mapping.ListLayers(md)
...     for layer in layerlist:
...         if layer.supports("DATASOURCE"):
...             print "layer name :"+layer.name+"source :"+layer.dataSource

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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