Detecting Corrupt MXDs in Python

3055
13
Jump to solution
04-09-2012 08:36 PM
JanetRogers
Occasional Contributor II
I have a python script that loops through all the MXDs in folder and subfolders and writes details of the layers to a text file.  The script works quite well, unless an MXD is corrupt at which point it fails (which I can completely understand!).   The corrupt MXD caused ArcGIS to crash when I tried to open the document in ArcMap and also when I attempted to preview it in ArcCatalog.  I am not sure if it can be done, however can anyone suggest a way to check the MXD before making an attempt to process it by the script.
Tags (2)
0 Kudos
13 Replies
JanetRogers
Occasional Contributor II
Thanks mvolz47.  I haven't solved this problems (as you said, I don't think this is possible) however it has enabled me to add some extra logging of the MXD names right at the beginning of the loop.  If the code fails, I now know which document is causing the problem and can eliminate it from  the folder.

Janet
0 Kudos
David_JAnderson
New Contributor III
Janet:

I have had to perform this task in older versions of ArcMap as well where python did not have its current capabilities so VBA was used.  In each case, whether it be in python or VBA, a corrupt mxd always crashed the script even with error handling such as try...except was used.  I have spoken with ESRI Technical representatives and they have confirmed that there is no way around a corrupt mxd in VBA or python scripting except to exclude it all together from your loop.

In my case, I had a script that would take an inventory of all mxds and various connections such as SDE.  This was a read-only step where information was saved to an output text file.  When the script would crash on a corrupt mxd, it would be noted and added to the code for the other script, that modified paths to data, so that script would just bypass these corrupt files and hopefully never crash.

I hope this information helps you to come up with a solution.


While doing the same type of mxd listing work I have found that arcpy has no problems in dealing with corrupt mxds when run from an ArcCatalog/ArcMap python command line.  However the same command run from a regular python window, IDLE or Pythonwin will cause python to crash.
map_doc = arcpy.mapping.MapDocument('path_to_mxd')
data_frames = arcpy.mapping.ListDataFrames(map_doc)

For a corrupt mxd, this code crashes in stand alone python but runs in the ArcCatalog/Map python window.
JanetRogers
Occasional Contributor II
Thanks, that is very useful information.

Janet
0 Kudos
AndrewQuee
Occasional Contributor III

We had a similar issue (and needed what sounds like the same script) relating to ArcGIS suddenly not liking shapefiles/rasters on a network drive, with the MXD/layers file crashing ArcMap/ArcCatalog instantly as soon as you tried to open them.  This happened across multiple installations and versions, all on the one day, and has actually occurred twice now.  It seemed as though ArcMap was corrupting something as it was writing to the document; possibly as a result of some network issue.

Crazily enough, the document was recoverable, by disconnecting the network adaptor or data source drive, then re-saving it - the new document worked absolutely fine, didn't even need to repair data connections.  Disabling the data source also allowed the following script to work:

#-------------------------------------------------------------------------------

# Name:        ExportMXDLayers.py

#

# Purpose:     List the layers of a specified mxd and attempt to save layer

#              files from the layers in that mxd

#

# Author:      Peter von Minden and Julian Ward

# Edited:      Andrew Quee

#

# Created:     22/08/2013

# Edited:      16/12/2013

#-------------------------------------------------------------------------------

import arcpy

errSummary = ""

succSummary = ""

# Must have a valid path to to the mxd in question using double backslashes "\\"

mxd = arcpy.mapping.MapDocument("D:\\Temp\\Export.mxd")

print "\nLAYER_NAME,DATA_SOURCE"

for lyr in arcpy.mapping.ListLayers(mxd):

    if lyr.supports("DATASOURCE"):

        print lyr.name + "," + lyr.dataSource

        try:

            # Must have a valid folder to dump the .lyr files using double

            # backslashes "\\"

            arcpy.SaveToLayerFile_management(lyr, "D:\\Temp\\" + lyr.name + ".lyr", "ABSOLUTE")

            succSummary += "Sucessfully Created " + lyr.name + ".lyr\n"

        except:

            errSummary += "Failed Creating " + lyr.name + ".lyr\n"

print "\n****SUMMARY FOR LAYER FILE CREATION****\n\n****SUCCESFUL****\n\n" + succSummary + "\n****ERRORS****\n\n" + errSummary

wait = raw_input("Press ENTER to continue")

(Sorry if it's a bit rough, it was literally hacked together in a few minutes while users were screaming about 'nothing working' to recreate their broken MXDs)

Coming back to the question, I'm not sure this is possible, David's tip aside.  In our experience, as soon as any Esri application touches a corrupted MXD, bang "ArcGIS Desktop has encountered a serious application error and is unable to continue."  This also applies to ArcPy, as you've found.

0 Kudos