I'm looking for a way to check for write access to an mxd upon the initiation of a python addin tool. My tool should only be enabled if the user has write access to the mxd. I thought there was a way to check for read/write access using arcpy (or maybe os?) but it's lost on me now. I tried the following as a work-around (saving upon initiation):

class ToolClass2(object):
    def __init__(self):
        mxd = arcpy.mapping.MapDocument ("CURRENT")
        try: mxd.save ()
        except IOError:
            pythonaddins.MessageBox ("The map document is locked.",
                                     "Map file locked", 0)
            self.enabled = False

However, this doesn't produce an IOError. Instead I get a warning box (below) from ArcMap and the try is successful and the except is ignored.

enter image description here

I've also tried making use of os.access, but without results (os.access (mapPath, os.W_OK) returns True):

mapPath = mxd.filePath
if not os.access (mapPath, os.W_OK):
    pythonaddins.MessageBox ("The map document is locked.",
                             "Map file locked", 0)
    self.enabled = False

I've now realized that I was thinking of arcpy.TestSchemaLock, but according to the help this is only for feature classes, tables, and feature datasets.

Developing with version 10.4 but the end users will be 10.1.

  • Just going to ask the obvious here, you have sufficient space on your hard drive? Also, we've had issues like this in a Citrix environment where the file server wasn't synchronizing with what we were seeing. – Fezter Jul 18 '17 at 4:33
  • 2
    I am almost sure you would need to use win32security from pywin32. stackoverflow.com/questions/896638/… – Alex Tereshenkov Jul 18 '17 at 5:14
  • 1
    Just try to open it for write access and catch the IO exception and do something else, pass, timer, whatever. open( filepath, 'w' ) – geodranic Jun 7 '18 at 15:52
  • @geodranic this method produces the IOError regardless of the current ArcMap's session write access. I can try this method and get the error while still able to execute mxd.save (). – Emil Brundage Jul 1 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.