How to unlock GeoPackage so can delete it

387
0
08-31-2022 01:18 PM
Eniac-6
New Contributor III

How can one remove a lock on a GeoPackage when used with a Python Toolbox? I have posted a similar question on GIS Stack Exchange.

 

My overall goal is to download a GeoPackage from online, and then create a shapefile copy of a subset of it (based on what area of the GeoPackage layer intersects another shapefile). After that, I don't need the large GeoPackage anymore and want to delete it. However, when trying to delete with os.remove,  I'm getting: PermissionError: [WinError 32] The process cannot access the file because it is being used by another process. When using arcpy.Delete_management, I get: ERROR 000601: Cannot delete gpkg. May be locked by another application. Here's a minimum reproducible example:

gpkg_wesm = 'my/output/directory/WESM.gpkg'
urllib.request.urlretrieve('https://rockyweb.usgs.gov/vdelivery/Datasets/Staged/Elevation/metadata/WESM.gpkg', filename = gpkg_wesm)
gpkg_ds = osgeo.ogr.Open(gpkg_wesm, update = 1)
lyr0 = gpkg_ds.GetLayer(0)
layer_wesm = os.path.join(gpkg_wesm, lyr0.GetDescription())     
arcpy.FeatureClassToFeatureClass_conversion(in_features = layer_wesm, out_path = 'my/output/directory', out_name = 'WESM.shp')
gpkg_ds = None
os.remove(gpkg_wesm)

 

0 Kudos
0 Replies