Monday, July 18, 2011

Testing for Schema Locks

TestingSchemaLock tests if a feature class or table has a schema lock.  Any tool that alters schema will require a schema lock to be placed on the input data.  If a tool requires a schema lock and is unable to acquire one an error message is provided.


import arcpy
data = arcpy.GetParameterAsText(0)
isLocked = arcpy.TextSchemaLock(data)
if lockTest:
   arcpy.AddField_management(data,"Flag","long")
else:
   print "Unable to acquire the necessary schema lock to add the new field"

2 comments:

cmorton said...

Thanks for the great posts! On Wednesday I used your post about getting the extent polygon to simplify my code. Then today I need to find away to check if a file already has a lock, and your post appears.

Sorry to nitpick, but there are a few issues with your example. The command is TestSchemaLock, not TextSchemaLock. You set it to 'isLocked', but then you test 'lockTest'. I went and looked at the online help and if the function returns True, the file does not have a schema lock, so it would be better to call your variable 'isNotLocked' maybe.

Anonymous said...

Its a really useful method for feature classes however I am yet to find something that allows you test for schema locks at the geodatabase level.

I resorted to using os commands to check for .lock files in the geodatabase container folder. Like to know if there was a better way.