2

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
1
  • 3
    It's a violation of best practice (and dangerous to geodatabase integrity) to load any spatial data as the SDE user. The SDE user should be reserved for enterprise geodatabase administration; one or more data ownership accounts should instead be created, with roles to manage access to still more user logins.
    – Vince
    Mar 3, 2016 at 1:24

1 Answer 1

4

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"
5
  • off-topic - is there a good reason why they've written it this way, or is it a bug/mistake? Mar 3, 2016 at 2:37
  • @StephenLead I'd say it has to be to do with how it's used in model builder. Prior to v9.3 it used to just return an int; It's definitely one of the more obscure and confusing cases in arcpy (and even ArcObjects). Mar 3, 2016 at 2:44
  • @StephenLead I think there was a decision made that every geoprocessing tool would return a result object so that we did not have to remember which did and which didn't. If I can find a reference I'll pass it on.
    – PolyGeo
    Mar 9, 2016 at 12:35
  • 1
    @StephenLead its here pro.arcgis.com/en/pro-app/arcpy/geoprocessing_and_python/…
    – PolyGeo
    Mar 9, 2016 at 12:39
  • 1
    @PolyGeo that does make a lot of sense. Clearly they put a lot of thought into it (and are much smarter than me!) Mar 14, 2016 at 10:54

Your Answer

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

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