Select to view content in your preferred language

ArcPy, Determine what version an MXD is?

10120
12
Jump to solution
08-02-2010 02:19 PM
RandyKreuziger
Frequent Contributor
I have an ArcPy python script that replaces data sources.  Right now I'm saving out the MXDs as 9.3
but ideally I want to save the repaired version out in the same version of ArcGIS that the original is in.  Is there a way to do this? 

If I don't specify the version my 9.3 mxds are saved out in version 10.
0 Kudos
12 Replies
JoeBryant1
Frequent Contributor

Hi William - thanks for keeping the thread alive!

I'm trying to get your code to work in the Python window in Arc, but (I think) the indenting is screwing me up. I keep getting "IndexError: list index is out of range" for the last line, which I take to mean that the result list is empty.

Is there a way to share the python code with the indent formatting in tact, like used to be possible on GeoNet?

FYI: I'm using ArcMap 10.7.1 and trying the code on an MXD (I believe) I saved down to 10.3.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

William's code was messed up, the indenting that is, in the transition from the old GeoNet hosting platform to the new hosting platform.  It happened to a lot of code.  Below is William's code reformatted.  Just note that MXDs with versions newer than 10.6 will likely cause code to fail unless you update the code to include newer versions.

 

Although fixing indentation is a nuisance, being able to do it successfully is a critical step in learning and understanding Python. 

import os, sys, arcpy, re

mxdFile = "C:\\temp\\map_document.mxd"
mxd101 = '10.1' + u'\u0013'
mxd102 = '10.1' + u'\u0013'
mxd103 = '10.3' + u'\u0014'
mxd104 = '10.4' + u'\u0015'
mxd105 = '10.5' + u'\u0016'
mxd106 = '10.6' + u'\u0017'

mxdList = [mxd101, mxd102, mxd103, mxd104, mxd105, mxd106]
result = []

with open(mxdFile, 'rb') as mxd:
    fileContents = mxd.read().decode('latin1')
    removedChars = [x for x in fileContents if x not in [u'\xff',u'\x00',u'\x01',u'\t']]
    joinedChars = ''.join(removedChars)
    for m in mxdList:
        location = joinedChars.find(m)
        if len(joinedChars[location:location+4]) > 0:
            result.append(joinedChars[location:location+4])
            print result[0]‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

0 Kudos
JoeBryant1
Frequent Contributor

Thanks for the quick fix, Joshua! That's the same indenting I ended up with, I was just doubting myself when I wasn't getting the results I expected.

I was running this on an MXD I thought I saved down to 10.3, using the saveACopy function from ArcPy. I was hoping to use William's code to verify it worked, but the result list is empty for any of the MXDs I save down with my custom Python Tool. However, I just tested William's code on an MXD I saved a copy of at 10.3 using the ArcMap 10.7.1 GUI, and his code did work, returning "10.3". So it looks like my saveACopy Python tool, based on the code here, is either not working properly, or the binary structure of the saved MXD's fileContents no longer matches the format this code is intended to interpret.

FYI: I've also added:

mxd107 = '10.7' + u'\u0018'

to the declared variables and added mxd107 to the mxdList, and can confirm that this will work on MXDs saved at 10.7.

Can we assume that mxd108 = '10.8' + u'\u0019' ?

0 Kudos