You have to remember the last parent chain.
If the next directory chain (your list in line 31) shares a common prefix with the last line you parsed, then remove only all those parents that do not match.
Example:
path list parents
/a/b/c/d -> [a,b,c,d] [p1, p2, p3, p4]
/a/b/e -> [a,b,e]
path list parents
/a/b/c/d -> [a,b,c,d] [p1, p2, p3, p4]
/a/b/e -> [a,b,e]
To copy to clipboard, switch view to plain text mode
Then you reduce parents to [p1, p2] and insert your new item under that chain.
You must not create a new parent chain all over for each line you parse.
Note: this algorithm assumes that the lines are sorted properly, which they should be in your case.
HTH
PS: I did not quite see where you "clear" your parent list.
Line 35 is beyond my understanding, too. Looks strange to me.
(I did not study your code in detail, sorry.)
Bookmarks