Select to view content in your preferred language

some stranges occurred when a featureclass in the same named dataset

630
4
09-25-2023 06:47 PM
pyfans
by
Emerging Contributor

there is a dataset named "DT" in geodatabase and two featureclasses named "A" and "DT" in the dataset,  and My code is:

arcpy.env.workspace=gdb

dts=arcpy.ListDatasets()

for dt in dts:

     fcs=arcpy.ListFeatureClasses(feature_dataset=dt)

     for fc in fcs:

           c=arcpy.GetCount_management(fc)

           fis=arcpy.ListFields(fc)

           fnames=[f.name for f in fis]

          arcpy.AddMessage('{} {} {}'.format(fc,c[0],';'.join(fnames))

 

After run this code, it'll print the correct name and count for both featureclasses but wrong for  'DT' featureclass's fields,  as get the same as  'A' featureclass, not actually 'DT' itself's.   

I use 10.8.2.     

Tags (3)
0 Kudos
4 Replies
DuncanHornby
MVP Notable Contributor

I'm really surprised that you can have a featureclass inside a featuredataset with the same name as the featuredataset. As a first test try renaming the DT featureclass to something else then run your code, what do you get then?

0 Kudos
SzymAdamowski
Occasional Contributor

I haven't heard about any rule prohibiting  feature class from having the same name as feature dataset - if there is one could you please provide source? Definitely you cannot have 2 feature classes with the same name in different datasets.

Back to the code - it worked well for me, but I have ArcGIS Pro, so there might be some specific bug in ArcMap. For clarity I put the code again in formatted way (please note that there is a missing parenthesis at the end of the original code by leigao):

arcpy.env.workspace=gdb
dts=arcpy.ListDatasets()
for dt in dts:
    fcs=arcpy.ListFeatureClasses(feature_dataset=dt)
    for fc in fcs:
        c=arcpy.GetCount_management(fc)
        fis=arcpy.ListFields(fc)
        fnames=[f.name for f in fis]
        print ('{} {} {}'.format(fc,c[0],';'.join(fnames))) 

 

0 Kudos
pyfans
by
Emerging Contributor

I have add a test gdb. If both of the featureclasses in dataset all not named as the same name with dataset, it correct, but when one featureclass is named same as dataset, it will print another featureclass's fields.

0 Kudos
SzymAdamowski
Occasional Contributor

Sorry for long time in responding. I tested your  code and database in ArcGIS Pro (3.2). Works well. If it doesn't work well in ArcMap then it might be ArcMap-specific bug. Code looks OK.

SzymAdamowski_0-1699702553928.png

 

0 Kudos