AnsweredAssumed Answered

Locks left on shapefiles when using SelectLayerBy tools

Question asked by detwiler on Mar 30, 2020
Latest reply on Mar 31, 2020 by Dan_Patterson

I recently switched from Desktop to Pro and have run into a problem with locks being left on shapefiles when I use SelectLayerByAttribute or SelectLayerByLocation.  Both the Desktop and Pro documentation show the same stand-alone script for both tools, though the Pro version of the script is a bit different. 

 

DESKTOP:

env.workspace = "c:/data/mexico.gdb"

# Make a layer from the feature class

arcpy.MakeFeatureLayer_management("cities", "lyr")

# Select all cities which overlap the chihuahua polygon

arcpy.SelectLayerByLocation_management("lyr", "intersect", "chihuahua", 0, "new_selection")

 

PRO:

arcpy.env.workspace = 'c:/data/mexico.gdb'

# Select all cities which overlap the chihuahua polygon

chihuahua_cities = arcpy.SelectLayerByLocation_management('cities', 'INTERSECT', 'chihuahua', 0, 'NEW_SELECTION')

 

The Desktop sample shows using MakeFeatureLayer to create a feature layer out of the cities feature class, then passing that as the first argument to the SelectLayerByLocation tool.  The Pro sample doesn't bother to create a feature layer first; it simply plugs in the feature class ('cities').   The Pro sample also shows storing a returned object in a variable (chihuahua_cities). 

 

When I've used these tools on shapefiles in Desktop, I've always used MakeFeatureLayer as shown in the sample.  It places a lock on the shapefile, but I can remove the lock by using Delete when I'm done (in this case, I'd do arcpy.Delete_management("lyr")). 

 

When I try to follow the syntax shown in the Pro help (storing the returned object in a variable), I also get a lock placed on the shapefile, but I haven't found a way to remove the lock aside from closing my IDE.  I've tried:

arcpy.Delete_management(chihuahua_cities)

arcpy.Delete_management(chihuahua_cities[0])

 

But neither actually removes the lock.  I can continue to code it the way that's shown in the Desktop help, but was wondering if there was a fix to the problem that follows the syntax shown in the Pro help.

 

Thanks!

Outcomes