I just need a bit of code that will look at the input feature class and return a value which is the parent OS folder that the GDB or SDE or other database resides in.
So far i have the following, but it returns the full path up to the feature dataset. I would like the code to return the folder path up until a geodatabase, And if the data is a shapefile, to just choose the parent folder.
import arcpy, os
from arcpy import metadata as md
dataset = arcpy.GetParameterAsText(0)
workspace = os.path.dirname(dataset)
walk = arcpy.da.Walk(workspace)
for dirpath, dirnames, filenames in walk:
arcpy.AddMessage(dirpath)
Greatly appreciated,
Solved! Go to Solution.
from pathlib import Path
def get_folder(dataset):
parent = Path(dataset).parent
if "." in parent.name: # .gdb, .sde
return str(parent.parent)
return str(parent)
from pathlib import Path
def get_folder(dataset):
parent = Path(dataset).parent
if "." in parent.name: # .gdb, .sde
return str(parent.parent)
return str(parent)