Search code examples
python-2.7arcpy

Arcpy Report all MXDs using specified Layer


I have a code I'm working on using python 2.7 and the arcpy plugin. The user will plug in a layer file and a root directory to search. The code will os.walk the directory and find every .MXD file. It will then search each .MXD file for the specified .SHP file. If an .MXD reports using the .SHP requested, it will log that .MXD file.

where lyr = E1.get() and is the .shp file being searched for.

lyr = E1.get()    
for root, dirs, files in os.walk(path):
                for name in files:
                    basename, extension = os.path.splitext(name)
                    if extension == '.mxd':
                        fullPath = os.path.join(root,name)
                        mxd = arcpy.mapping.MapDocument(fullPath)
                        DataList = arcpy.mapping.ListLayers(mxd)
                        for item in DataList:
                            if item == lyr:
                                LOG_ME = mxd
                                l.info(LOG_ME)
                            else:
                                pass
                    else:
                        pass

The logfile is created when the program runs, but never populates any data. I receive no errors, even in a directory I know contains .MXDs that use the specified .SHP I've also tried

for item in DataList:
    if lyr in item:
        log

and

if lyr in DataList:
    log

any ideas what the issue might be?


Solution

  • I believe the problem may be in your comparison

    if item == lyr:

    Note that arcpy.mapping.ListLayers returns a list of layer objects, not a list of layer names.

    Try changing your comparison to

    if item.name == lyr:

    Good luck!

    Tom