# Listing Data sources using arcpy: ListLayers very slow

What I am trying to do is create a list of all the data sources in a directory filled with mxds. So I have this code pasted below. For some reason when listing layers it can take up to 50 minutes even though the mxd only has around 10 or 15 layers. Is this a common occurrence with the listlayers method?

with open('data.csv', 'wb') as csvfile: fieldnames = ['File_Path', 'File_Size(MB)', 'Last_Edit', 'MXD_Version', 'Author', 'PageSize',  'XMAX', 'XMIN', 'YMAX', 'YMIN', 'DATASOURCE'] #all the header fields writer = csv.DictWriter(csvfile, fieldnames = fieldnames) writer.writeheader() for mapDocument in findFiles(r"W:/GIS (no purge)", '*.mxd'): print mapDocument st = os.stat(mapDocument) print "Using ArcPY Mods… " mxd = arcpy.mapping.MapDocument(mapDocument) #using arcpy to gather more information print "Reading Map Document… " writer.writerow({'File_Path' : str(mapDocument), 'File_Size(MB)' : str(((st[ST_SIZE])/1024.0)/1024.0),  'Last_Edit' : str(time.ctime(st[ST_MTIME])),  'MXD_Version' : getMXDVersion(mapDocument)}) print "Searching Layers and Writing… " for layer in arcpy.mapping.ListLayers(mxd): print "boop" if layer.supports("DATASOURCE"): writer.writerow({'DATASOURCE' : layer.dataSource}) else: break del mxd

So my issue with the above code is when I am running it It will take forever to get to the 'print "boop"'but once it gets there it runs perfectly fine. It seems to get hung up at thearcpy.mapping.ListLayers(mxd)for a very long time.

Sometimes its great, only takes a few seconds, other times 8 minutes. That is manageable, but when I leave my code running all night and some of them take 50 minutes or it just crashes, that is the problem (it crashed due to a memory error). These mxds do not have that many layers in them either.

There is usually just one data frame that has at the most 20 layers in them. I can open these mxds myself and it only takes a few minutes at the most when i do.

From further testing it doesn't seem to be related to the amount of layers either. There were a few mxds that had around 30 layers that were completed in a few seconds and then another few that took 4 hours (it had 44 layers) and there are some that had 30 layers and took 50 minutes. If I could just pinpoint why it takes so long it would be great.

First, check your script against a directory with one mxd and one layer, to make sure that the problem isn't something in the part of your script that you didn't post. Next, you need to modify your script to test for more "layer.supports" properties besides "dataSource". I use the version below:

for lyr in lyrList: dType = "UNK" dName = "UNK" dPath = "UNK" if lyr.supports("dataSource"): if lyr.supports("workspacePath"): dPath = lyr.workspacePath if lyr.supports("datasetName"): dName = lyr.datasetName try: dType = arcpy.Describe(lyr.dataSource).datasettype except: dType = "Possible broken link" else: if lyr.isGroupLayer: dType = "Group Layer" if lyr.supports("SERVICEPROPERTIES"): arcpy.AddMessage('Found map survice') dType = lyr.serviceProperties["ServiceType"]