Script get parent path including root

I have a list of tags that I want to determine their parent...
For the first two examples, I usually use something like this:

tag.replace('\\','/')

[MyTP]first/second/third, parent = [MyTP]first/second
[MyTP]first/second, parent = [MyTP]first

But, that doesn't work for the root. Does anyone have a nifty way to get a parent including the root?
[MyTP]first, parent = [MyTP]

Might be easiest to use the os.path module
10.1. os.path — Common pathname manipulations — Python 2.7.18 documentation

from os import path
print path.dirname('[MyTP]first/second/third')

Use the TagPathParser to parse the string into a TagPath.

from com.inductiveautomation.ignition.common.tags.paths.parser import TagPathParser

path = "[MyTP]first/second/third"
tagPath = TagPathParser.parse(path)
print 'Provider :=', tagPath.source, 'ParentPath :=', tagPath.parentPath

Output:

>>> 
Provider := MyTP ParentPath := [MyTP]first/second
>>> 

EDIT: I think that I didn't fully answer your question, in your case where the path supplied is '[MyTP]First' then the output is:

>>> 
Provider := MyTP ParentPath := [MyTP]
>>> 
7 Likes

Perfect, a much cleaner solution.