How to Count Feature Classes in a Dataset?

6054
2
04-26-2011 06:09 PM
MichaelClarke
New Contributor III
Hi,

I'm trying to write a script to count the number of feature classes in a dataset so that I can delete any datasets that have a count of 0 feature classes.

this forms the last part of the script below:
import arcpy
from arcpy import env

# Sets the workspace
env.workspace = "L:\TEMP\test_Database_working.gdb"

# lists all feature classes in the workspace (geodatabase)
listFCs = arcpy.ListFeatureClasses("*")
# lists all feature datasets in the workspace (geodatabase)
listFDs = arcpy.ListDatasets("*")

# loops through the Feature Class list and deletes any with 0 features
for fc in listFCs:
    count1 = str(arcpy.GetCount_management(fc))
    if count1 == "0":
        arcpy.Delete_management(fc)

# loops through the Feature Dataset list and deletes any with 0 feature classes
# this part contains errors but shows what I'm attempting to acheive
for dataset in listFDs:
    count2 = str(arcpy.GetCount_management(dataset))
    if count2 == "0":
        arcpy.Delete_management(dataset)       


How can I fix the last part as 'GetCount_management' does not work?

Thanks!
Mick
Tags (2)
0 Kudos
2 Replies
FrédéricPRALLY
Esri Contributor
Hi Mick,

In the script below I put a list to count fc in dataset. If there are no fc in the dataset, you can delete it. I also append a check if fc in dataset is empty or not.

See script:

import arcpy
from arcpy import env

# Sets the workspace
env.workspace = r"L:\TEMP\test_Database_working.gdb"

# lists all feature classes in the workspace (geodatabase)
listFCs = arcpy.ListFeatureClasses("*")
# lists all feature datasets in the workspace (geodatabase)
listFDs = arcpy.ListDatasets("*")

# loops through the Feature Class list and deletes any with 0 features
for fc in listFCs:
    count1 = str(arcpy.GetCount_management(fc))
    if count1 == "0":
        arcpy.Delete_management(fc)

# loops through the Feature Dataset list and deletes any with 0 feature classes
# this part contains errors but shows what I'm attempting to acheive
for dataset in listFDs:
    count = 0
    for fc in arcpy.ListFeatureClasses("*", "", dataset):
        count2 = str(arcpy.GetCount_management(fc))
        if count2 == "0":
            arcpy.Delete_management(fc)
        else:
            count +=1
    if count == 0:
        print "Delete dataset: " + dataset
        arcpy.Delete_management(dataset)
        
print "End process"


Hope this help,

Best regards,

Fred
0 Kudos
MichaelClarke
New Contributor III
Thanks Fred, works perfectly!

now I need to work out what its doing so I can learn something 😄

Cheers,
-Mick
0 Kudos