Select to view content in your preferred language

Locks left on shapefiles when using SelectLayerBy tools

3288
12
Jump to solution
03-30-2020 02:07 PM
JimDetwiler1
Emerging Contributor

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!

1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

FAQ: Why am I getting schema locks on shapefiles? 

effectively, the observations are as expected since they don't persist on process close

View solution in original post

0 Kudos
12 Replies
DanPatterson_Retired
MVP Emeritus

If you are running a script and not doing the process line-by-line, then you shouldn't have issues with locks.

You might want to try deleting the python variables if you have them.  

Most IDE's have a 'delete variable names' and a restart kernel (like Spyder) so you don't need a complete shutdown of the IDE (although it provides a suitable coffee break time)

0 Kudos
JimDetwiler1
Emerging Contributor

Thanks for the quick reply.  I had been running line by line, but just tried running it normally and the locks are still there.  I had also already tried del variable, but that doesn't remove the lock either.  And I discovered the Restart option in Spyder because of this issue.  🙂

0 Kudos
DanPatterson_Retired
MVP Emeritus

hmmmm

Select Layer By Location—Data Management toolbox | Documentation 

Determines how the selection will be applied to the input and how to combine it with an existing selection. Note that there is no option here to clear an existing selection. To clear a selection, use the CLEAR_SELECTION option on the Select Layer By Attribute tool.

Since you are doing it line by line, why not whip in a

Select Layer By Attribute—Data Management toolbox | Documentation 

with the above clearing the selection to see if that does anything

I assume that you have Pro open when you are doing this, which may also be an issue.

0 Kudos
JimDetwiler1
Emerging Contributor

No effect after adding SelectLayerByAttribute with CLEAR_SELECTION.  Yes, I've had Pro open.  I just tried again with Pro closed, but no difference.

The documentation says the tools return a Result object, so I've also tried using getOutput(0) and using Delete on that object.  And I've tried doing setSelectionSet([]).  No luck.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Turn it into a script, run it.  Python won't lock it, and if the lock is there it is a Pro issue.  

Also, dump the shapefile and try it with a featureclass in a gdb

0 Kudos
JimDetwiler1
Emerging Contributor

Sorry if I wasn't clear.  I am running a script in Spyder.  I've tried stepping through it with the debugger and I've tried just running it normally with the same result. 

And what I'm talking about happening is a .lock file appears in my Windows File Explorer as soon as I run SelectLayerByLocation or SelectLayerByAttribute. 

I just put my data into a file GDB and ran the script on it with the same result.  When I look inside the .gdb folder, a .lock file appears after running SelectLayerByLocation.  If I try using Delete on the object returned by the tool, the .lock file persists.

However, if I run these tools the way they're demonstrated in the Desktop help -- creating a layer using MakeFeatureLayer, using the layer as an input to the tool, then running Delete on the layer when I'm done with it -- the  lock file immediately disappears.

Maybe you could try it and see if you get the same behavior?
import arcpy

lyr= arcpy.SelectLayerByAttribute_management(yourFC, 'NEW_SELECTION', yourWhereClause)

arcpy.Delete_management(lyr)

0 Kudos
DanPatterson_Retired
MVP Emeritus
import arcpy

f = r"C:\Git_Dan\npgeom\Project_npg\npgeom.gdb\Polygons2"

res = arcpy.SelectLayerByAttribute_management(f, "NEW_SELECTION", '"OBJECTID" > 1')

res[0]
<arcpy._mp.Layer at 0x18f06bc35f8>

res[1]
'2'

res.getInput(0)
'C:\\Git_Dan\\npgeom\\Project_npg\\npgeom.gdb\\Polygons2'

res.getInput(1)
'NEW_SELECTION'

res.getOutput(0)
<arcpy._mp.Layer at 0x18f06bc5fd0>

res.cancel()

del res

Lock remained until the kernel was restarted. 

Project not opened, lock file had a size of 0 bytes.

Delete_management did squat.

del res killed it in python and wasn't available after line 24.

So script or no script.... locks remain until the calling/using thread was terminated.. which sort of makes sense to me, just in case you might want to use the selection.

clear_selection, cleared it, but produces another result (getting a headache) and the lock remained.

Restarting the kernel killed the process and the lock went away (and I got coffee).

Now what I didn't do was try Copy_management to save the result to disk to see if that freed the lock.

DanPatterson_Retired
MVP Emeritus

FAQ: Why am I getting schema locks on shapefiles? 

effectively, the observations are as expected since they don't persist on process close

0 Kudos
JimDetwiler1
Emerging Contributor

Thanks again for your help, Dan.  For background, I teach an introductory Python scripting class and have always operated under the assumption that scripts shouldn't leave locks in place when they've finished.  I see that after running my script I can open Pro and edit a shapefile that had a lock created on it by my script, so I guess it's a "benign" one.  Still, it would be nice if I didn't have to try to explain that locks are sometimes a problem, sometimes not.