Is there any smart way to combine overlapping paths in python? -


let's have 2 path names: head , tail. can overlap number of segments. if don't i'd join them normally. if overlap, i'd detect common part , combine them accordingly. more specific: if there repetitions in names i'd find long overlapping part possible. example

"/root/d1/d2/d1/d2" + "d2/d1/d2/file.txt" == "/root/d1/d2/d1/d2/file.txt" , not "/root/d1/d2/d1/d2/d1/d2/file.txt" 

is there ready-to-use library function such case, or have implement one?

you can use list comprehension within join function :

>>> p1="/root/d1/d2/d1/d2" >>> p2="d2/d1/d2/file.txt" >>> p1+'/'+'/'.join([i in p2.split('/') if not in p1.split('/')]) '/root/d1/d2/d1/d2/file.txt' 

or if difference base name of second path can use os.path.basename bname , concatenate p1 :

>>> import os >>> p1+'/'+os.path.basename(p2) '/root/d1/d2/d1/d2/file.txt' 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -