[GIS] How to use ArcPy to list feature class and number of rows per feature class

arcgis-10.0arcpy

I would like to iterate a geodatabse to get the name of each feature class/data set in it, in addition to the number of rows in the feature class. I would also like to identify whether the dataset that is being printed is a feature class or feature dataset.

The code that I have now prints each feature class and feature data set; but does not denote whether if the object printed is a feature class or feature data set. How would I print the number of rows per feature class in addition to denote whether if it is a feature class or feature dataset?

I am using ArcGIS Desktop 10.0 and a direct connect via spatial database.

import arcpy   
arcpy.env.workspace='myworkspace'

    datasetList = arcpy.ListDatasets("*", "Feature")
    for dataset in datasetList:
        print dataset
        fcList = arcpy.ListFeatureClasses("*","",dataset)
        fcList.sort()
        for fc in fcList:
            print fc
        for fc in fcList:
            arcpy.GetCount_management(fc)

Sample Output:

SDEDEV.SDE.Boundaries
SDEDEV.SDE.CTYA_GLEN
SDEDEV.SDE.ZipCode
SDEDEV.SDE.Streets
SDEDEV.SDE.CAMS_Streets_2012
SDEDEV.SDE.ROW
SDEDEV.SDE.Streets_COG

Best Answer

The docs for GetCount don't say it very well but it returns a gp result object and not a number.. to get a number (see example 2) you need to use getOutput(0) and int.

Your script (modified):

import arcpy   
arcpy.env.workspace='myworkspace'

    datasetList = arcpy.ListDatasets("*", "Feature")
    for dataset in datasetList:
        print dataset
        fcList = arcpy.ListFeatureClasses("*","",dataset)
        fcList.sort()
        for fc in fcList:
            cnt = int(arcpy.GetCount_management(fc).getOutput(0))
            print "{0} {1}".format(fc,cnt) # print in the format "fcname count"