Solved! Go to Solution.
.... in loop [INDENT]try: mxd = mapping.MapDocument(mxdPath) #.... do something except: #write to file which map documents are incorrect[/INDENT]
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
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.
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.
"append" was the key I was missing! Thanks, it's always the little things!