views:

245

answers:

5
Python 2.6 (trunk:66714:66715M, Oct  1 2008, 18:36:04) 
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> path = "/Volumes/Users"
>>> path.lstrip('/Volume')
's/Users'
>>> path.lstrip('/Volumes')
'Users'
>>>

I am expecting output of path.lstrip('/Volumes') should be /Users

+5  A: 

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/")

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

I suspect you need to use slicing instead:

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

but hopefully someone with more intimate knowledge of the Python library might give you a better option.

Lasse V. Karlsen
That is the "one obvious way to do it".
THC4k
+8  A: 

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
that's not true, docs: *The `chars` argument is a string specifying the set of characters to be removed. *
SilentGhost
@SilentGhost: sorry, my mistake, I edited it.
tuergeist
`'/home/user/Volumes/important_path'.replace('/Volumes', '', 1)` -->`'/home/user/important_path'`
Mark Rushakoff
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
yes. I know... str.replace was just an example for this specific example from Vijayendra Bapte.
tuergeist
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
@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
+1  A: 

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
+9  A: 

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
A: 

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 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

inspectorG4dget