Get feature class names inside Esri file geodatabase

I'm not sure exactly where your script is running off the rails, but I did notice a few things. Your fist elif on line 9 should probably be just an if. and it is giving me an illegal target error. Second, in the else statement on line 15 you are tacking on function calls to your file list which i'm not sure you want.

Try this. os.walk is a beautiful little function that walks through a directory returning all found paths, directories and files. It takes care of a lot of the path and search nonsense for you.

https://docs.python.org/3/library/os.html#os.walk

import os
import arcpy

search_directory = r'C:\Projects'

# small function to walk through directory and check all .gdbs in a folder.
def directory_walk(directory_to_search):
    for path, dirs, files in os.walk(directory_to_search):
        for dir in dirs:
            if dir.lower().endswith(".gdb"):
                # Generator magic, pops off one record at a time.
                yield os.path.join(path, dir)



feature_list = []
for gdb in directory_walk(search_directory):
    arcpy.env.workspace = gdb
    fc = arcpy.ListFeatureClasses("AOI*", "Polygon")
    feature_list.extend(fc)  # add found Feature classes to file list

print(feature_list)

If you wanted the file paths instead you could loop through the feature list and append the path returned from os.walk to get paths like so

import os
import arcpy

search_directory = r'C:\Projects'

# small function to walk through directory and check all .gdbs in a folder.
def directory_walk(directory_to_search):
    for path, dirs, files in os.walk(directory_to_search):
        for dir in dirs:
            if dir.lower().endswith(".gdb"):
                # Generator magic, pops off one record at a time.
                yield os.path.join(path, dir)



feature_list = []
for gdb in directory_walk(search_directory):
    arcpy.env.workspace = gdb
    fc = arcpy.ListFeatureClasses("AOI*", "Polygon")
    for f in fc:
        feature_list.append(os.path.join(gdb, f))

print(feature_list)

os.path.join takes care of all the mess of dealing with // and \ and what nots as well.


You can easily access geodatabases and featureclasses with fiona and glob using a couple lines of code. In this example, glob is used to list all of the geodatabases in a directory dir. fiona.listlayers() is used within in a list comprehension to iterate through all the featureclasses in each geodatabase with the "AOI" condition. Finally, itertools flattens the list so that all of the featureclasses are in a single list.

import fiona
import os, itertools, glob

dir = '/path/to/your/directory'

# List all GDB's recursively
gdbs = glob.glob(os.path.join(dir, '**/*.gdb'), recursive = True)

# List all featureclasses in each GDB IF 'AOI' is in the featureclass name
fcs = list(itertools.chain.from_iterable([[os.path.join(y,x) for x in fiona.listlayers(y) if 'AOI'.lower() in x.lower()] for y in gdbs]))

This will produce a list of featureclasses with "AOI" anywhere in the featureclass name, for example:

['/dir1/geodatabase1.gdb/Aoi1',
 '/dir1/geodatabase1.gdb/AOI2',
 '/dir1/dir2/geodatabase2.gdb/xyz_AoI',
 '/dir3/dir4/dir5/geodatabase6.gdb/another_one_aoi_xyz']