1

I found some conversation about listing the source paths for multiple mxd files and used the script provided at Crawling directory and listing all datasources using ArcPy?.

It worked great on one folder but on the other two folders I get this error:

Traceback (most recent call last): File "\de1-s07\Q-Drive\Jobs\Missouri\I_70_PEL_StLouis\AECOM\Map_Docs_MxdCrawler\mxdcrawler.py", line 33, in main(folderPath, output) File "\de1-s07\Q-Drive\Jobs\Missouri\I_70_PEL_StLouis\AECOM\Map_Docs_MxdCrawler\mxdcrawler.py", line 9, in main w.writerows(rows) File "\de1-s07\Q-Drive\Jobs\Missouri\I_70_PEL_StLouis\AECOM\Map_Docs_MxdCrawler\mxdcrawler.py", line 18, in crawlmxds dataframes = arcpy.mapping.ListDataFrames(mxd) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\mapping.py", line 1496, in ListDataFrames result = mixins.MapDocumentMixin(map_document).listDataFrames(wildcard) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\mixins.py", line 730, in listDataFrames

Each time I run the script it fails at a different point. One time it only read 2 files and this time it read 9 files. The python script is:

import arcpy, os, csv

def main(folder, outputfile):
    with open(outputfile, "wb") as f:
        w = csv.writer(f)
        header = ("Map Document", "MXD Path", "DataFrame Name", "DataFrame Description", "Layer name", "Layer Datasource")
        w.writerow(header)
        rows = crawlmxds(folder)
        w.writerows(rows)

def crawlmxds(folder):
    for root, dirs, files in os.walk(folder):
        for f in files:
            if f.lower().endswith(".mxd"):
                mxdName = os.path.splitext(f)[0]
                mxdPath = os.path.join(root, f)
                mxd = arcpy.mapping.MapDocument(mxdPath)
                dataframes = arcpy.mapping.ListDataFrames(mxd)
                for df in dataframes:
                    dfDesc = df.description if df.description != "" else "None"
                    layers = arcpy.mapping.ListLayers(mxd, "", df)
                    for lyr in layers:
                        lyrName = lyr.name
                        lyrDatasource = lyr.dataSource if lyr.supports("dataSource") else "N/A"
                        seq = (mxdName, mxdPath, df.name, dfDesc, lyrName, lyrDatasource);
                        yield seq
                print "running"
                del mxd

if __name__ == "__main__":
    folderPath = r"\\de1-s07\Q-Drive\Jobs\Missouri\I_70_PEL_StLouis\AECOM\Map_Docs\Restart" # or arcpy.GetParameterAsText(0)
    output = r"\\de1-s07\Q-Drive\Jobs\Missouri\I_70_PEL_StLouis\AECOM\Map_Docs\_mxdcrawler\mxdcrawler_Restart.csv" # or arcpy.GetParameterAsText(1)
    main(folderPath, output)

print "done"

What do I need to fix to get this to work?

I am using python 2.7.8 through ArcGIS 10.3 GUI.

closed as too broad by PolyGeo May 7 '18 at 22:34

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Is the layer valid? (not lyr.isBroken) Are you sure the layers support dataSource? (lyr.supports("dataSource"). Group layers (lyr.isGroupLayer) don't and some other layer types. Check if lyr.isFeatureLayer or lyr.isRasterLayer before trying to get the datasource from it. resources.arcgis.com/en/help/main/10.2/index.html#/Layer/… – Michael Stimson May 7 '18 at 21:53
  • "Each time I run the script it fails at a different point." suggests that there may be multiple issues with your script perhaps depending on the data that it is encountering. For focused Q&A I think you need to isolate one mxd and one layer that an error is occurring on. To do that I would start by adding some print statements that indicate the map and layer that it is trying to process when failing. – PolyGeo May 7 '18 at 22:37
  • 1
    I was trying to answer but am not familiar enough with how this forum is structured. Michael, your questions got me looking at this problem at a higher level; that I was just going through all of the files. It ended up failing at the same file that was saved by the client in a newer version than what I have on my computer. I took that file out of my list and viola! the script worked. I have another folder with the same error so I now know what to look for. Thanks for making me think broader. The failing at different places was caused by some renaming, which I had not connected. Teresa – geonerd May 7 '18 at 22:45
  • @geonerd a shortcut to quickly absorbing how a focused Q&A site like this differs from a discussion forum can be found at gis.meta.stackexchange.com/a/3353/115 – PolyGeo May 7 '18 at 23:12
  • Oh that dreaded no forward compatibility problem.. I suggest you use a try/catch (or is it try/except in python) at the stage of mxd = arcpy.mapping.MapDocument(mxdPath), it should fail there or on the next line, and return an error message with arcpy.AddWarning("{} is either newer or broken".format(mxdPath)) so you can check these ones manually - if you're running from the toolbox you'll get green text for the MXDs' that can't be opened... hopefully. – Michael Stimson May 8 '18 at 2:16