2

I wrote a script that creates a GDB using a designated parish/county name and then does a selection by location and extracts from another db to the newly created db. This all works fine but there are some empty feature classes in the db that I want to remove.

The code below recognizes the empty fc's but does not delete them. In the results, it creates the message saying that it deleted the fc but when I check the geodatabase everything was stored in, the empty fc's are still there. This portion of the code runs perfectly fine when adjusted for other scripts that just involve shapefiles in a folder. Is it an issue with the gdb that I'm not aware of?

 arcpy.env.workspace = output + r'/' + parish + '.gdb'
for file in arcpy.ListFeatureClasses():
 layerName = arcpy.Describe(file).baseName
 arcpy.MakeFeatureLayer_management(file, layerName)
 if int(arcpy.GetCount_management(layerName).getOutput(0)) == 0:
 arcpy.Delete_management(file)
 arcpy.AddMessage('Deleted "{}" because it contains no features!'.format(file))
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked May 7, 2015 at 19:48

3 Answers 3

4

Some tips:

  • There is no need to create feature layers to count the features;
  • Don't use the file as the variable name - this is a reserved class name;
  • Avoid concatenating strings when constructing folder/data paths; use os.path.join instead.

import arcpy
import os
folder = r"C:\Documents\ArcGIS\scratch"
geodb = r"imported_data.gdb"
arcpy.env.workspace = os.path.join(folder,geodb) #output + r'/' + parish + '.gdb'
for fc in arcpy.ListFeatureClasses():
 #layerName = arcpy.Describe(fc).baseName
 #arcpy.MakeFeatureLayer_management(fc, layerName)
 if int(arcpy.GetCount_management(fc).getOutput(0)) == 0:
 arcpy.Delete_management(fc)
 arcpy.AddMessage('Deleted "{}" because it contains no features!'.format(fc))
answered May 7, 2015 at 20:42
1
  • Thanks a lot, Alex! I always appreciate some tips to improve my arcpy kung fu! Commented May 8, 2015 at 15:42
3

It looks like that should have done the trick...but a couple of things. In the Zen of Python, explicit is better than implicit. Even though setting the arcpy.env.workspace should be good enough for deleting feature classes, I have found that it is always best to provide the full path for all GP tools.

Also, you do not need to make a feature layer to count the records in each feature class. Try this:

import os
import arcpy
arcpy.env.workspace = out_ws = os.path.join(output, parish + '.gdb')
for fc in arcpy.ListFeatureClasses():
 if int(arcpy.GetCount_management(fc).getOutput(0)) == 0:
 arcpy.Delete_management(os.path.join(out_ws, fc))
 arcpy.AddMessage('Deleted "{}" because it contains no features!'.format(fc))
answered May 7, 2015 at 20:33
2

In this case, your feature classes and layers have the same name. And because you're using arcpy.env.workspace, you are deleting something... the layer.

When a geoprocessing tool tries to resolve data parameters, they will first look to see if the 'data' exists as is, and it will as the layer name. If that doesn't turn up anything, the next check would be to use the input value combined with the workspace, but it already found and deleted the layer with the previous check.

Trick here would be to give the layer name something unique like: layerName = "{}_layer".format(arcpy.Describe(file).baseName). Or be explicit like @crmackey rightly suggested. Or in your particular case, you don't actually need to create a layer at all (GetCount will work just fine on the feature class), so skip that part.

answered May 7, 2015 at 20:44
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.