2

I work with ArcGIS 10.3 and I try to search a specific layer (with dataSource named "D:\desktop\Project\layers\1.jpg" ) in hundred of mxd's that spread in folder "D:\PROJECTS" and it divided to hundred of sub folders, using python 2.7.8:

import arcpy,os,sys
import arcpy.mapping
from arcpy import env

env.workspace = r"D:\PROJECTS"
for mxdname in arcpy.ListFiles("*.mxd"): 
    mxd = arcpy.mapping.MapDocument(r"D:\desktop\Project\\" + mxdname)
    dfList = arcpy.mapping.ListDataFrames(mxd, "*")
    for df in dfList:
        for lyr in arcpy.mapping.ListLayers(mxd, "", df):
            if lyr.isGroupLayer == True: continue                                   
            if lyr.dataSource == r"D:\desktop\Project\layers\1.jpg":
                print mxdname, mxdname.pathway
    mxd.save()
del mxd

Finally, I want that python will print all the mxd's source name that contain the specific layer I search for. When I run the code I get error:

project1.mxd

Traceback (most recent call last):
  File "C:\Users\yaron.KAYAMOT\Desktop\idle.pyw", line 13, in <module>
    print mxdname, mxdname.pathway
AttributeError: 'unicode' object has no attribute 'pathway'
2

From the listfiles docs:

Returns a list of files in the current workspace based on a query string

So mxdname does not have a pathway attribute. It's simply a string of the mxd name.

1

Finally, i used this code:

import arcpy  
from arcpy import mapping as m  
from os import path, walk  

root_directory = r"D:\PROJECTS"  
path_to_find = r"F:\GIS\topo_50000\50000.sid"  
def FindMaps(root_directory, path_to_find):
maps = []  
for root, dirnames, filenames in walk(root_directory):  
    for fname in [f for f in filenames if f.endswith(".mxd")]:  
        mxdPath = path.join(root, fname)  
        if not path.isfile(mxdPath):  
            continue  

        mxd = m.MapDocument(mxdPath)
        df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
        for df in m.ListDataFrames(mxd):  
            for lyr in m.ListLayers(mxd, data_frame=df):  
                if lyr.supports("DATASOURCE"):  
                    if lyr.dataSource == path_to_find:  
                        print(mxdPath)  
                        maps.append(mxdPath)  
                        break            
return maps

FindMaps(root_directory, path_to_find)

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.