I'm looking for a way to check for writing access to an mxd upon the initiation of a python AddIn tool. My tool should only be enabled if the user has writing 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, 2017 at 4:33
  • 2
    I am almost sure you would need to use win32security from pywin32. stackoverflow.com/questions/896638/… Jul 18, 2017 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, 2018 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 (). Jul 1, 2019 at 20:35
  • What technique did you use to restrict access? Is this file on a network drive that a user can't edit, is it a read-only permission on the file itself? Is the user part of a particular security group? I'm not sure the answer changes depending on how you've restricted that file, but it might be useful to know.
    – derivative
    Feb 26, 2020 at 21:13


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.