result = arcpy.GetCount_management(fc) print result if result == 0: print fc + �?� is empty.�?� arcpy.delete_management(fc)
result = arcpy.GetCount_management(lyr) count = int(result.getOutput(0)) if count == 0: arcpy.mapping.RemoveLayer(df, lyr) print "Removing " + lyr else: print "Keeping " + lyr
The result object's getOutput method returns a Unicode string from result objects that have output values. This is an important consideration when running a tool such as GetCount, which provides a count of records in a table, or CalculateDefaultClusterTolerance, which provides a cluster tolerance value. To convert the value to the expected type, it will have to be converted from a unicode string using built-in Python functions such as int() or float().
############################################################## ## Delete Empty Feature Classes from Multiple Geodatabases ## ## This script loops through all geodatabases within a directory ## and removes empty featureclasses from each geodatabase. ## ## User must hardcode directory. Run this script in IDLE. ## ## Python Version 2.4 ############################################################## # Create the Geoprocessor object import arcgisscripting, os, sys, string gp = arcgisscripting.create() # Load required toolboxes�?� gp.AddToolbox("C:\\Program Files\\ArcGIS\\ArcToolbox\\Toolboxes\\Conversion Tools.tbx") # Set workspace gp.workspace = "L:\\Justin\\00_jobs\\Python_Scripting_Test\\" # list personal geodatabases in the current workspace listmdb = gp.listworkspaces("*", "FileGDB") mdb = listmdb.next() # loop through the personal geodatabases while mdb: print mdb gp.workspace = mdb # list feature datasets in personal geodatabase listfdset=gp.listdatasets() fdset=listfdset.next() # loop through feature dataset while fdset: print "fdset: ", fdset if not fdset == None: gp.workspace = mdb + "\\" + fdset # list feature classes in the personal geodatabase fcs = gp.listfeatureclasses() fcs.reset() fc = fcs.next() # loop through the featureclasses while fc: print fc, gp.GetCount_management(fc) # if the feature class table has no records, delete the featureclass if gp.GetCount_management(fc) == 0: gp.Delete_management(fc) print fc + "deleted from" + mdb fc = fcs.next() fdset=listfdset.next() mdb = listmdb.next()
listmdb = arcpy.ListWorkspaces('*', 'FileGDB') for mdb in listmdb: print mdb # same logic as above # change inner loops to for loops too # don't call next, since it's done behind the scene with the for loop
listmdb = gp.ListWorkspaces('*', 'FileGDB') for mdb in iter(listmdb.next, None): # Note: no function parantheses following next print mdb # same logic as above # change inner loops to for loops too # don't call next, since it's done behind the scene with the for loop