How to remove a path prefix in python?


I wanted to know what is the pythonic function for this :

I want to remove everything before the wa path.

p = path.split('/')
counter = 0
while True:
    if p[counter] == 'wa':
    counter += 1
path = '/'+'/'.join(p[counter:])

For instance, I want '/book/html/wa/foo/bar/' to become '/wa/foo/bar/'.

Asked By: Natim



>>> path = '/book/html/wa/foo/bar/'
>>> path[path.find('/wa'):]
Answered By: Felix Loether
import re

path = '/book/html/wa/foo/bar/'
m = re.match(r'.*(/wa/[a-z/]+)',path)
Answered By: vivek

A better answer would be to use os.path.relpath:

>>> import os
>>> full_path = '/book/html/wa/foo/bar/'
>>> relative_path = '/book/html'
>>> print(os.path.relpath(full_path, relative_path))
Answered By: Mitch ミッチ

For Python 3.4+, you should use pathlib.PurePath.relative_to.
From the documentation:

>>> p = PurePosixPath('/etc/passwd')
>>> p.relative_to('/')

>>> p.relative_to('/etc')

>>> p.relative_to('/usr')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "", line 694, in relative_to
    .format(str(self), str(formatted)))
ValueError: '/etc/passwd' does not start with '/usr'

Also see this StackOverflow question for more answers to your question.

Answered By: pjgranahan
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.