How to retrieve the workspace path from a layer using arcpy?

4184
3
04-20-2017 09:31 AM
AmirBar-Maor
Esri Regular Contributor

Jason Camerano‌ gave me this python snippet:

head, sep, tail = arcpy.Describe(parcelFabricLayer).Path.partition(".gdb")
workspacePath = head + sep

explanation:

Describe().Path returns the full path that also contains the string ".gdb"

partition separates is using the provided ".gdb" string  to:

  • head - before the ".gdb" string
  • sep - the ".gdb" string
  • tail - after the ".gdb" tring

Anyone has a better method?

0 Kudos
3 Replies
JulianInskip
Occasional Contributor

Hi Amir.

I do mine a little different. I don't use arcpy to get my workspace path. I find mine takes a lot quicker as it does not have to call arcpy every time, saving a few seconds. With this method you can also use any geodatabase type (.sde, .gdb or .mdb).

accessRoadFc = r"D:\Data\GISData\data\received\Transport.gdb\Routing\AccessRoad"
wkspSplit = accessRoadFc.split(".")
wkspExt = wkspSplit[1].split("\\")[0]
workspace = "{}.{}".format(wkspSplit[0], wkspExt)‍‍‍
MattHowe
Occasional Contributor

For Julian's path example I'd normally use os.

a = r"D:\Data\GISData\data\received\Transport.gdb\Routing\AccessRoad"
os.path.dirname(os.path.dirname(a))
'D:\\Data\\GISData\\data\\received\\Transport.gdb'
0 Kudos
mdonnelly
Esri Contributor

In Pro you can use the following:

layer.connectionProperties["connection_info"]["database"]
Regards,
Mark
0 Kudos