Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of os.path.commonprefix

Tags:

python

path

What's the opposite of os.path.commonprefix? I have two paths and I want the non-overlapping path, e.g.:

>>> p1 = '/Users/foo/something'
>>> p2 = '/Users/foo/something/else/etc'
>>> print somefunction([p1, p2])
'/else/etc'
like image 825
Puzzled79 Avatar asked Dec 16 '11 07:12

Puzzled79


1 Answers

>>> p1 = '/Users/foo/something'
>>> p2 = '/Users/foo/something/else/etc'
>>> os.path.relpath(p2, start=p1)
'else/etc'

The correct answer is 'else/etc' and not '/else/etc'.

If you are in p1 and type cd /else/etc you wouldn't land in p2, but somewhere else.

os.path.join(p1, 'else/etc') gives you p2 again.

like image 192
eumiro Avatar answered Oct 26 '22 18:10

eumiro