Select to view content in your preferred language

Save path to gdb from GetParameterAsText

749
7
10-18-2017 01:37 AM
Nicole_Ueberschär
Esri Regular Contributor

I wrote a tool script using python that collects a couple of feature classes (from different Feature data sets in the same gdb) as several GetParameterAsText. 

When I now want to start editing I figured out that it requires to address the gdb and not the Feature data set (or the feature class). But all methods I found until now refer to the feature data set. 

like

os.path.split(_polygonfeature)[0]

or 

os.path.dirname(_polygonfeature)

Isn't there a way to call the gdb that contains the feature class/feature data set?

Thanks in advance!

Tags (2)
0 Kudos
7 Replies
KevinDunlop
Occasional Contributor III

I believe there is arcpy method that does this but an easy way is to search the string for the .gdb.

strTest = r"C:\WorkDoc\Default.gdb\myDataset\myFeatureClass"
pathGDB = strTest[:strTest.index('.gdb')+4]‍‍
print pathGDB‍‍‍
Nicole_Ueberschär
Esri Regular Contributor

Thanks Kevin, this is at least a solution. 

I still also believe that there must be an arcpy method...

0 Kudos
DanPatterson_Retired
MVP Emeritus
0 Kudos
Nicole_Ueberschär
Esri Regular Contributor

Thanks for the hint Dan, but for me the catalogPath gives the full path to the feature class and not only the gdb.

Also baseName is giving the feature class (without the path).

0 Kudos
DanPatterson_Retired
MVP Emeritus

Slice and splice...

cp = r"C:\Git_Dan\a_Data\testdata.gdb\square2"  # ---- catalogPath result -----

to_cp = "\\".join(cp.split(".")[0].split("\\")[:-1])  #---- split, slice, split, slice

to_cp
'C:\\Git_Dan\\a_Data'
0 Kudos
Nicole_Ueberschär
Esri Regular Contributor

Yes, that goes in the same direction as Kevin's solution. I still think there must be a property somewhere storing the gdb automatically...

0 Kudos
DanPatterson_Retired
MVP Emeritus

a gdb is essentially a folder, so 'os.path' isn't going to be much help either

cp = r"C:\Git_Dan\a_Data\testdata.gdb"
os.path.isdir(cp)
True
0 Kudos