Select to view content in your preferred language

List Broken Data Source's Path

10759
46
08-07-2013 09:49 AM
LauraMiles1
Frequent Contributor
Hi everyone, I'm a very amateur Python user. I've managed to get the following, which I pieced together from here and there, working for the most part. I need to create a csv file which has a column showing the path to the data source which is broken. Everything works fine and dandy if I take out the dataSource part; when I add it in, it will run for a while and then fail. It seems to be getting tripped up on some mxd or data source it doesn't like perhaps? I have no clue. Here's my code:

import arcpy, os
path = r"H:\Plans\GIS Plans\2003"
f = open('BrokenMXD2003.csv', 'w')
f.write("Type, File Path, Layer, Broken Path" + "\n")
for root, dirs, files in os.walk(path):
    for fileName in files:
        basename, extension = os.path.splitext(fileName)
        if extension == ".mxd":
            fullPath = os.path.join(root, fileName)
            mxd = arcpy.mapping.MapDocument(fullPath)
            brknMXD = arcpy.mapping.ListBrokenDataSources(mxd)
            for brknItem in brknMXD:
                lyrList = arcpy.mapping.ListLayers(mxd)
                f.write("MXD, " + fullPath + ", " + brknItem.name)
                if brknItem.supports("dataSource"):
                    f.write(", " + brknItem.dataSource + "\n")
                else:
                    f.write("\n")

f.close()

print "Script Completed"


And here's the error I get:

Traceback (most recent call last):
  File "X:\Documents\Working Files\Broken Data Sources\ListBrokenMXD.py", line 11, in <module>
    brknMXD = arcpy.mapping.ListBrokenDataSources(mxd)
  File "D:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "D:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\mapping.py", line 1465, in ListBrokenDataSources
    result = mixins.MapDocumentMixin(map_document_or_layer).listBrokenDataSources()
  File "D:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 832, in listBrokenDataSources
    broken_sources = [l for l in self.layers if not l._arc_object.valid]
  File "D:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 683, in layers
    for frame in reversed(self.dataFrames):
  File "D:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 695, in dataFrames
    return map(convertArcObjectToPythonObject, self.pageLayout.dataFrames)
AttributeError: 'NoneType' object has no attribute 'dataFrames'

It does run and create several lines of output, but this error appears when it gets near to the end of the mxd's in the directory. I haven't a clue what the problem could be, as I said I'm quite amateur. If anyone can see what it is, I'd be greatly appreciative. Thank you!
Tags (2)
0 Kudos
46 Replies
StacyRendall1
Frequent Contributor
It's not foolproof, but you can do a try...except, i.e.:
try:
  brokenItem.supports("dataSource")
  write dataSource
except AttributeError:
  write ... # something


Try...except basically catches the error and lets you do something esle. If it is a tableView it will cause an attribute error (according to your earlier post), and you can write something different to the file. If there is another issue with the data source, such as an IOError, it will still crash normally.
0 Kudos
StacyRendall1
Frequent Contributor
Looks like two threads were merged? Not seeing a reason.


I think the wrong threads were merged. Someone had posted the same question twice:
http://forums.arcgis.com/threads/90182-Max-Date-How-to-return-unique-IDs-with-Max-Date (now directs to this thread)
http://forums.arcgis.com/threads/90171-Create-layer-of-records-with-Max-Date

For some reason the merge was applied to the wrong two threads...
0 Kudos
lelaharrington
Regular Contributor

Thank you i will try this in the next coming weeks and report back my findings.

0 Kudos
lelaharrington
Regular Contributor

ok so I tired this. here is my script. it still never got past the first three mxd's with the last one having a broken table source in it.

yes yes I can fix that table... but I need this to walk thru an entire F: and tell me where all and any broken sources are... even if it is just noting " hey there is a broken source here"

Broken Sources.png

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Hi Lela,

I have a script that will eventually be released with my Python addin, but this script,  listing of broken links, works.  It gets around all the tables, and other strange things.  As is, you should only have to change theWorkspace to a folder you want to test.  Let the rest go to default until you understand what it does.

I've been able to let is run on a folder/subfolders for about 14 hours ....looked at 2217 mxd's, found 51k+ broken linkjs, of which about 3k are broken.  Many, like "groups" are list, and then can be ignored.  I'm working on the "repair" script, but keep running into the 10.3.1 to older version Addin compatibility (and it's driving me a bit batty).  Give this a try (use a test area to see what it does)   If you don't put anything for the inFile parameter, it only writes log files.  I'll include a couple utility files that will need to be in the same folder....or in your python bin.

I attached them in a zip rather than posting the script.  I'll be out for a couple hours, but if you have questions or problems, I can help later today.

0 Kudos
lelaharrington
Regular Contributor

Thank you. I am interested to see the repair script as that is my goal as well. to repair some of the broken sources. I know that some I will have to manually repair but if I can identify all broken sources and repair like 50 % of them

even that is better than manually opening each one.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I'm still working on getting a few more of the anomalies worked out.  I have a variety of  broken sources (coverages or all types, shapes, SDE) that are having to change....some to different types.  I will post when it's complete, but not ready to release yet....a few too many things I still need to muddle thru.

0 Kudos