Select to view content in your preferred language

Get parent folder of GDB as a string.

1121
1
Jump to solution
09-06-2022 07:36 AM
David_Brooks
MVP Regular Contributor

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, 


David
..Maps with no limits..
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
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)

Have a great day!
Johannes

View solution in original post

1 Reply
JohannesLindner
MVP Frequent Contributor
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)

Have a great day!
Johannes