string.lstrip stripping too much?

Roy Smith roy at panix.com
Sun May 15 13:41:07 EDT 2005


"joram gemma" <joram.gemma at pandora.be> wrote:

> Hello,
> 
> on windows python 2.4.1 I have the following problem
> 
> >>> s = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona'
> >>> print s
> D:\music\D\Daniel Lanois\For the beauty of Wynona
> >>> t = 'D:\\music\\D\\'
> >>> print t
> D:\music\D\
> >>> s.lstrip(t)
> 'aniel Lanois\\For the beauty of Wynona'
> >>> 
> 
> why does lstrip strip the D of Daniel Lanois also?

Because the argument to lstrip is a *set of characters* to delete, not a 
string to delete.  The string you passed it contained a 'D', so the 'D' got 
stripped.  Imagine lstrip was defined something like:

def lstrip (self, chars):
   temp = self
   while temp[0] in chars:
      temp = temp[1:]
   return temp

and you should get the idea.  I don't think the documentation for lstrip 
really makes this clear.  I'm going to open a bug on the doc, and see what 
happens :-)

I suspect what you really want to be doing is using the os.path module.  
It's got functions for tearing pathnames apart into components, and hides 
all the uglyness like whether / or \ is the directory separator on your 
particular system.



More information about the Python-list mailing list