5

I'm trying to find a way to determine whether a feature class is locked before trying to alter it.

In one database I want to remove a feature class completely, and in another I want to add extra fields. I can't do either if they're locked.

I was expecting to find something in arcpy.Describe() but haven't found anything. Something like

if arcpy.Describe(myFC).isLocked:
    print "Can't proceed - feature class is locked"
else:
    arcpy.Delete_management(myFC)

Is there a way to check feature class locks using arcpy?

9

Found it - I was using incorrect terminology in my searches.

There is a tool called Test Schema Lock which will return True or False based on whether a lock can be applied or not. So my code would be:

if not arcpy.TestSchemaLock(myFC):
    print "Can't proceed - feature class is locked"
else:
    arcpy.Delete_management(myFC)

Because it returns True if the feature class isn't locked (i.e. a lock can be applied), an if not needs to be used to find where it returns False (it is locked).

  • 1
    Does this work on the entire gdb as well as individual feature classes? What about shapefiles? – Craig T Apr 19 '17 at 13:57
  • @CraigT it's only 4 lines, have you tried it? I suspect it will work for shapefiles as well, but I haven't tried. I don't know about entire gdb - locks are usually per feature class so I'm not sure if a gdb reports it is locked. You could loop through each feature class to determine which is locked and which isn't – Midavalo Apr 19 '17 at 14:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.