Deterimine version of MXD from Python?

468
10
12-20-2012 09:09 AM
BillWiesepape
New Contributor II
I'm using ArcGIS 10.0 and arcpy.mapping.ListLayers to iterate through the layers in a list of MXDs. my problem is that someon has saved a 10.1 mxd somewhere in the list of MXDs I'm working through. I'd be fine with just skipping that file, but how do I determine what version a MXD file is from Python?
0 Kudos
10 Replies
curtvprice
MVP Esteemed Contributor
I'm using ArcGIS 10.0 and arcpy.mapping.ListLayers to iterate through the layers in a list of MXDs. my problem is that someon has saved a 10.1 mxd somewhere in the list of MXDs I'm working through. I'd be fine with just skipping that file, but how do I determine what version a MXD file is from Python?


Does opening a 10.1 mxd generate an error, as I would expect? In that case the truly Pythonic way is to try it and then ask for forgiveness:

for mxdPath in mxdList:
    try:
        mxd = arcpy.mapping.MapDocument(mxdPath)
        ## < do your stuff >
    except:
        print "Could not open the map document: \"%s\"" % mxdPath
0 Kudos
BillWiesepape
New Contributor II
Sounded great, until I tried it. Then it didn't work. Below is some sample code. When Python gets to processing the second file it crashes.

import arcpy

pathList = []

pathList.append(r"\\server\share\MyVersion10.mxd")
pathList.append(r"\\server\share\MyVersion10_1.mxd")

for eachPath in pathList:
    try:
        MXD = arcpy.mapping.MapDocument(eachPath)
        for lyr in arcpy.mapping.ListLayers(MXD):
            print(lyr.name)
    except:
        print("failed on " + eachPath)
0 Kudos
ChrisFox3
Regular Contributor II
Do you know if it crashes when you create a reference to the mxd or when you attempt to list the layers? I find I can get a reference to a 10.1 mxd from 10.0 fine, but then when it try to do stuff it raises exceptions, doesn't explain the crash, but might help us workaround it.
0 Kudos
BillWiesepape
New Contributor II
Chris-

As you describe, it makes connection to the 10.1 MXD just fine, it's once you try to list the layers that the script crashes.
0 Kudos
curtvprice
MVP Esteemed Contributor
Chris-

As you describe, it makes connection to the 10.1 MXD just fine, it's once you try to list the layers that the script crashes.


Please define "crash". Does ArcMap die?
0 Kudos
BillWiesepape
New Contributor II
I'm running the script in IDLE. When it crashes IDLE exits with the message "pythonw.exe has stopped working".

I get the feeling that the correct answer is "use 10.1" and that will work fine, until 10.2 comes out. There has to be some way to determine if the MXD you are connected to is valid that will at least respect the "try" block instead of completely bombing python.

-Bill
0 Kudos
curtvprice
MVP Esteemed Contributor
There has to be some way to determine if the MXD you are connected to is valid that will at least respect the "try" block instead of completely bombing python.


Couldn't agree more. The only other approach I would suggest is quite kludgy - find some content in the 10.1 file that is unique to 10.1, open the file as a string, and look for it. I sure hope Chris or someone else has a better idea!

The better, pythonic solution would be to the mxd object not fail in such an ugly way when it finds unexpected data!
0 Kudos
ChrisFox3
Regular Contributor II
Which service pack of 10.0 do you have installed? I am on service pack 5 and like you I find creating a reference to the MXD is fine, but unlike you an exception is raised when i attempt to list the layers and the try/except succesfully handles it. You may want to try installing sp5 as we may have resolved a bug that resolves this crash. If you are already at sp5 I will have to think about this a little more to come up with an alternative approach.
0 Kudos
BillWiesepape
New Contributor II
We are SP4. I'll see if we can get the OK to move to SP5 and see if that fixes the problem.
0 Kudos