1

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))
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.

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))
3

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))
  • Thanks a lot, Alex! I always appreciate some tips to improve my arcpy kung fu! – TacoB0t May 8 '15 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.