Select to view content in your preferred language

Make Layer Spatial Join Exclusive Lock Error

493
1
03-31-2012 01:27 PM
TurnerNowak
Deactivated User
Can anyone help me remove the lock on Parcels.shp in this script.  I get the exclusive lock error on the
gp.Delete_management(fc + "\\Parcels.shp")
part of the script because Parcels.shp is most likely being used in the feature layer above it. I tried deleting the layer, but that does not work.

#
#
import arcgisscripting
import logging
import os
import sys, string

logger = logging.getLogger()

# Create the geoprocessor object
gp = arcgisscripting.create(9.3)
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
gp.OverWriteOutput = 1

# Set the workspace. List all of the folders within
gp.Workspace = "C:\\ZP44"
fcs = gp.ListWorkspaces("*","Folder")


#
for fc in fcs:
    
    gp.MakeFeatureLayer(fc + "\\Parcels.shp","lyr")
    gp.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", "\"SIT_FULL_S\" >''")
    situscount = str(gp.GetCount_management("lyr").getoutput(0))
        
    if situscount == '0':
        print 'NO SITUS IN DATA'    
    else:
        gp.Delete(lyr, "layer") 
        print fc
        gp.AddSpatialIndex_management(fc + "\\Parcels.shp", "0", "0", "0")
        gp.SpatialJoin_analysis(fc + "\\Parcels.shp", 'C:\\ESRI\\ESRIDATA\\USA\\usa_zipcodes.shp', fc + "\\Parcelsjoined.shp", "")
        gp.CalculateField_management(fc + "\\Parcelsjoined.shp", "SIT_ZIP", "[POSTAL]", "VB", "")
        gp.CalculateField_management(fc + "\\Parcelsjoined.shp", "SIT_CITY", "[CITYNAME]", "VB", "")
        gp.DeleteField_management(fc + "\\Parcelsjoined.shp", "Join_Count;Join_Cou_1;Join_Cou_2;POSTAL")
        gp.Delete_management(fc + "\\Parcels.shp")
        gp.Rename_management(fc + "\\Parcelsjoined.shp", "Parcels.shp")
     



And here is the error:

Traceback (most recent call last):
  File "C:\Python24\ADDZIPNEW.PY", line 40, in <module>
    gp.Delete_management(fc + "\\Parcels.shp")
ExecuteError: ERROR 000464: Cannot get exclusive schema lock.  Either being edited or in use by another application.
Failed to execute (Delete).
Tags (2)
0 Kudos
1 Reply
KimOllivier
Honored Contributor
I have played around a little and have discovered that to delete a FeatureLayer you need to name the type properly, ie "FeatureLayer", not "Layer"

import arcpy

arcpy.env.workspace = "d:/workspace"
fc = "addmar06.shp"
if not arcpy.Exists(fc):
    print fc,"not found"
arcpy.env.overwriteOutput = True # so I can rerun to recreate a layer 
# make an in-memory layer
result = arcpy.management.MakeFeatureLayer(fc,"fc_layer")
print "this is the string-name of the fc:",result.getOutput(0)
# but it is not the object
# find the datatype of the layer with the name of "fc_layer"
desc = arcpy.Describe("fc_layer")
print "Datatype is:",desc.datatype
# now delete the featurelayer using the proper datatype
try:
    result2 = arcpy.management.Delete("fc_layer","FeatureLayer")
    print "Has it gone?",result2.getOutput(0)
    # prove it has gone
    arcpy.Describe("fc_layer")
except Exception,errmsg:
    print "Error because:",errmsg
    # but fc still intact
    if arcpy.Exists(fc):
        print "Yes, fc still there"


>>> this is the string-name of the fc: fc_layer
Datatype is: FeatureLayer
Has it gone? true
Error because: "fc_layer" does not exist
Yes, fc still there


I also found that if you used arcpy.management.Delete(fc,"ShapeFile") it deleted it for me even with a Layer defined.
0 Kudos