22
>>> path = "/Volumes/Users"
>>> path.lstrip('/Volume')
's/Users'
>>> path.lstrip('/Volumes')
'Users'
>>> 

I expected the output of path.lstrip('/Volumes') to be '/Users'

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Vijayendra Bapte
  • 1,378
  • 3
  • 14
  • 23

5 Answers5

36

lstrip is character-based, it removes all characters from the left end that are in that string.

To verify this, try this:

"/Volumes/Users".lstrip("semuloV/")  # also returns "Users"

Since / is part of the string, it is removed.

You need to use slicing instead:

if s.startswith("/Volumes"):
    s = s[8:]

Or, on Python 3.9+ you can use removeprefix:

s = s.removeprefix("/Volumes")
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • That is the "one obvious way to do it". – Jochen Ritzel Nov 06 '09 at 12:22
  • To more efficiently remove the prefix, use split, with maxsplits=1 `path = "/Volumes/Users" drive = "/Volumes" if path.startswith(drive): path = path.split(drive, 1)[1]` – Kevin Feb 14 '19 at 19:46
17

Strip is character-based. If you are trying to do path manipulation you should have a look at os.path

>>> os.path.split("/Volumes/Users")
('/Volumes', 'Users')
Nadia Alramli
  • 111,714
  • 37
  • 173
  • 152
  • These days, use [`pathlib`](https://docs.python.org/library/pathlib.html#pathlib.PurePath.name) instead: `from pathlib import Path` and then `Path("/Volumes/Users").name` or `Path("/Volumes/Users").parts[-1]` – Boris Verkhovskiy Nov 25 '20 at 07:16
16

The argument passed to lstrip is taken as a set of characters!

>>> '   spacious   '.lstrip()
'spacious   '
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'

See also the documentation

You might want to use str.replace()

str.replace(old, new[, count])
# e.g.
'/Volumes/Home'.replace('/Volumes', '' ,1)

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

For paths, you may want to use os.path.split(). It returns a list of the paths elements.

>>> os.path.split('/home/user')
('/home', '/user')

To your problem:

>>> path = "/vol/volume"
>>> path.lstrip('/vol')
'ume'

The example above shows, how lstrip() works. It removes '/vol' starting form left. Then, is starts again... So, in your example, it fully removed '/Volumes' and started removing '/'. It only removed the '/' as there was no 'V' following this slash.

HTH

tuergeist
  • 9,171
  • 3
  • 37
  • 58
  • 2
    `'/home/user/Volumes/important_path'.replace('/Volumes', '', 1)` --> `'/home/user/important_path'` – Mark Rushakoff Nov 06 '09 at 12:16
  • Or `'/home/user/Volumes_for_my_mp3s/jazz'.replace('/Volumes', '', 1)` --> `'/home/user_for_my_mp3s/jazz'`. See what you're missing yet? :) – Mark Rushakoff Nov 06 '09 at 12:22
  • yes. I know... str.replace was just an example for this specific example from Vijayendra Bapte. – tuergeist Nov 06 '09 at 12:22
  • If it doesn't apply to the general case, then say so: maybe *his* example was just *one* of the hundred cases he needs to run this on. – Mark Rushakoff Nov 06 '09 at 12:24
  • @Mark: I wrote: 'Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.' – tuergeist Nov 06 '09 at 13:54
1

lstrip doc says:

Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping

So you are removing every character that is contained in the given string, including both 's' and '/' characters.

Eemeli Kantola
  • 5,437
  • 6
  • 35
  • 43
0

Here is a primitive version of lstrip (that I wrote) that might help clear things up for you:

def lstrip(s, chars):
    for i in range len(s):
        char = s[i]
        if not char in chars:
            return s[i:]
        else:
            return lstrip(s[i:], chars)

Thus, you can see that every occurrence of a character in chars is is removed until a character that is not in chars is encountered. Once that happens, the deletion stops and the rest of the string is simply returned

Nissa
  • 4,636
  • 8
  • 29
  • 37
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241