Good point Michael. Michael Volz
As we've had a couple of issues like this as well, we tend to use this method a lot now for each operation:
with open(path + 'Log_' + Date + '.log', 'a') as output: #open log file in append mode. output.write(logentry) output.close()
Although this slows down the process quite a bit, it's better to know exactly what happened and at what point rather than the script simply crashing and not flushing the output cache to file.
Edit: Added code format style to snippet, for some reason it wouldn't let me choose this when wrote my reply.
Kari:
Although this adds another step to your process, I would run through the files that you plan to process without actually resourcing your data and log the files that you have read through. If the script crashes, you know it is the next file in the directory so you can exclude that file by name (or delete it entirely if you find it totally corrupted). I would perform this process by subfolders of your main folder so if the script crashes on a corrupt mxd you don't need to go through as many files in your next run. This was an important step in resourcing my organization's mxds and lyr files which numbered in the 10s of thousands and had 100s of thousands of data connections that needed to be resourced. I hope this information helps you through this tedious but important process to your endusers.
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.
Michael,
Can you share how you noted when an mxd was corrupt? I'm also working on a program that walks through mxds in a folder to determine if a particular layer is used or not and writes it to a csv file but the script crashes when it encounters a corrupt mxd. But it crashes before the list is written to the csv file so I never know which one it is.
Any guidance would be great
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.
map_doc = arcpy.mapping.MapDocument('path_to_mxd') data_frames = arcpy.mapping.ListDataFrames(map_doc)
.... in loop [INDENT]try: mxd = mapping.MapDocument(mxdPath) #.... do something except: #write to file which map documents are incorrect[/INDENT]
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.