Select to view content in your preferred language

ListFeatureClasses vs. Walk with shapefile

498
1
08-22-2023 12:30 AM
marzue
by
New Contributor

Dear All,

I am trying to do something very simple : listing the feature class stored in a shapefile. My first idea was to use the "ListFeatureClasses" function as  I usually do with GDB files. However this return an empty list

my_shp = r"mydir\my_shapefile.shp"
arcpy.env.workspace = my_shp
arcpy.ListFeatureClasses()
# this does not work and returns []

However, when I use the "Walk" function, I am able to retrieve the feature classe :

walk = arcpy.da.Walk(my_shp, datatype="FeatureClass", type="Polygon")
feature_classes = []
for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))
feature_classes
## this indeed returns me the fullpath to my feature classes
# [mydir\my_shapefile.shp\fcname]

Why is the "ListFeatureClasses" function not returning "[fcname]" in this case ? 

Is it another "List[...]" function (or another arcpy function) that would directly return "[fcname]" for this shapefile to alleviate the need to iterate over a walk ?

Sorry, it's surely a naive question, but I am quite new to arcpy and shapefiles especially...

Thanks in advance if you have any suggestion...

 

0 Kudos
1 Reply
DanPatterson
MVP Esteemed Contributor

A shapefile is a featureclass.  It only contains one geometry type and resides in a folder, whereas a geodatabase can contain many featureclasses of different names. When you use listfeatureclasses on the shapefile, it is not a "workspace" (eg folder or gdb) that is why it returns an empty list as in your first example.. In the second example using "walk" you get the full path to the featureclass and its name in the list


... sort of retired...