23 Dec

Python Code Sample: Get the Geodatabase Path From a Feature Class or Table Path

In ArcGIS Python projects, we are often required to parse the path of a feature class or table to get the geodatabase portion of the path. For example, we need to return:

“c:\work\gisdata.gdb”
from
“c:\work\gisdata.gdb\usa\roads”

Here is a function we use:

def get_geodatabase_path(input_table):
  '''Return the Geodatabase path from the input table or feature class.
  :param input_table: path to the input table or feature class 
  '''
  workspace = os.path.dirname(input_table)
  if [any(ext) for ext in ('.gdb', '.mdb', '.sde') if ext in os.path.splitext(workspace)]:
    return workspace
  else:
    return os.path.dirname(workspace)
   

The performance of this approach is much greater than that of the arcpy.Describe() method.

FacebookTwitterGoogle+Share