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"
This works great. the only question i have and since I am new at this is have you found a way to get this script to walk thru all sub folders and look or will i have to go thru each folder in the directory with this script?
Thank you
Lela Harrington
Hi Lela, if I recall it does go through all the subfolders. So just change the path where indicated in the script and it will go through all folders/subfolders etc.
I tried to just do like H: and to pool thru sub folders and it sadly won't talk thru them all
Lela Harrington
What happened, did it freeze? I just tried running it on one of our main folders and it worked. It stopped when it hit a table view data source, but it was running through subfolders...
yup i got the table view error that is what happened
but it doesn't tell me where the table was
"Traceback (most recent call last):
File "H:/PythonTesting/OS_Walk_Testing/List_Broken_Data_Sourse_Path.py", line 18, in
if brknItem.supports("dataSource"):
AttributeError: 'TableView' object has no attribute 'supports'"
Thank you
Lela Harrington
Yes now that I recall, and looking through the rest of the thread, I sort of stopped working on the script without solving this error. The test for the table view actually causes the script to crash when it does encounter a table view (or so the error suggests).
I tried modifying things so that it only writes the dataSource if the file extension is not .dbf, but it still crashes with the same tableview error. I inspected the .mxd it crashed halfway through and there aren't actually any tableviews in it. So there is something else within the .mxd that's causing this to happen. Testing for the .dbf extension is not weeding out the problem...I ran the .mxd through MXD Doctor and there seems to be nothing wrong with it. I'm stumped.
I'm running into this issue to (started with AttributeError: 'NoneType' object has no attribute 'dataFrames'). From my testing, it looks like it has to do with older MXD's (mine was from around 2010) that have something that's somehow not compatible with the arcpy stuff. I tried Save As and Save a Copy and the problem persisted. Copying and pasting the dataframe doesn't work either. However, if I create a new dataframe in the MXD and copy the layers into it and delete the old data frame, it works fine; so it can't be something with the MXD in general.
I think the solution is to print the MXD path and try to manually fix the dataframe(s) in there before continuing. If you have a lot of MXD's and touching each one isn't reasonable, the next best solution is probably what Stacy Rendall mentioned about using a try, except block to catch the AttributeError exception and skip that MXD if there's an error. Not the cleanest, but it works.
try: # Do stuff with MXD using arcpy.mapping except AttributeError: print "This MXD has unreadable dataframes\n{}".format(mxd.filePath) pass
I was getting this error "AttributeError: 'NoneType' object has no attribute 'dataFrames'" today in a script that I was running against all STARTED services in AGS (10.2.2). I'm looping thru all those services, finding the MXDs and searching to see if the FGDB I'm needing to update is in the MXD, then creating a list of the services I need to STOP so I could replace the FGDB and then restart. This error was showing up on only one of my services (and therefore crashing). It turns out the for some reason, that mxd must be saved as 10.3.x, or so the error seems to indicate if I try to open with ArcMap 10.2.2.
The error occurred with "lyrList = arcpy.mapping.ListLayers(mxd)" so I wrapped in in a ListDataFrames loop, with no luck. Since there is no easy or reliable way to check for the version of an .mxd (from several hours of searching and testing a half dozen suggestions), I found the try: my best option, and I'll just include this service in my STOP list, just in case.
Thought I would give this thumbs up on the try: solution for anyone that might see this....we do what we need to get things done sometimes Snippet of my code, still under development for anyone interested. (will not run on its own)
# .... def myMsgs(message): arcpy.AddMessage(message) print(message) updateServices = [] for startedService in myServices: mxdFound = False update = False #myMsgs("Reviewing service: {0}".format(startedService)) theServicePath = os.path.join(servicesDir, startedService) if not arcpy.Exists(theServicePath): myMsgs("...we have a problem") exit else: myMsgs("Reviewing service: {0}".format(startedService)) for root, dirs, files in os.walk(theServicePath): for fn in files: fullPath = os.path.join(root, fn) basename, extension = os.path.splitext(fn) if not extension == ".mxd": pass else: #myMsgs(" mxd file found") mxd = arcpy.mapping.MapDocument(fullPath) try: # Do stuff with MXD using arcpy.mapping for df in arcpy.mapping.ListDataFrames(mxd): lyrList = arcpy.mapping.ListLayers(mxd) mxdFound = True for lyr in lyrList: if lyr.isFeatureLayer and update == False: if lyr.supports("dataSource") and update == False: theSource = lyr.dataSource if updateDS[0] in theSource: update = True myMsgs(" update {0}".format(startedService)) updateServices.append(startedService) except AttributeError: myMsgs("!!!! WARNING: This MXD has unreadable dataframes\n{} !!!!".format(mxd.filePath)) myMsgs(" -> will stop as a precaution....") updateServices.append(startedService) # edit....forgot this on original post pass del mxd for a in updateServices: myMsgs("will stop {}".format(a)) # ....
Python Beginner - Using version 10.1 and not an SDE install I have an atribute table with firehydrant inspection data. I am trying to create a layer that will only show the records grouped by fire hydrant number and then only display the most recent inspection date. When I execute the following code it runs fine and does in fact create the layer but it doesn't select the Max Date. If I have a record for Hydrant 100 with two dates I only want the most recent date to be created within the new layer.
Appreciate any assistance on this.import arcpy from arcpy import env env.workspace=r"Z:\GIS_Data\gdb\GIS_Test.gdb" fc="COA_FH_Inspections" cursor = arcpy.UpdateCursor(fc) idlist = [] datelist = [] for row in cursor: idlist.append(row.getValue('HydrID')) datelist.append(row.getValue('TestDate')) cursor.updateRow(row) print idlist print datelist idunique = set(idlist) #Make a layer from the feature class arcpy.MakeFeatureLayer_management(fc, "lyr") for i in idunique: if i == idlist: arcpy.SelectLayerByAttribute_management("lyr", "New_Selection", '"HydrID" = i') arcpy.SelectLayerByAttribute_management("lyr", "subset_Selection",'"TestDate" = (Select Max("TestDate") from "lyr"')
Again thank you - Terry