Stripping parts of a path

Larry Bates larry.bates at websafe.com`
Sat Jul 26 09:59:18 EDT 2008


Tim Cook wrote:
> Hi All,
> 
> I just ran into an issue with the rstrip method when using it on path
> strings.
> 
> When executing a function I have a need to strip off a portion of the
> current working directory and add on a path to a log file.  Initially
> this worked great but then I added a branch in SVN which caused the path
> to contain 'LNCCWorkshop'.  The rstrip() then began removing the
> characters 'shop' leaving an incorrect path to the log file.  When I
> hard coded this path it worked okay but then did the same thing later in
> the file when I needed to point to a database. The code worked fine with
> a different path.  Here are some code fragments. 
>  
> logfile=os.getcwd().rstrip('src/oship/atbldr')+'/oship/log/at_build_errors.log'
> 
> this worked when the path was: 
> /home/tim/ref_impl_python/TRUNK/oship/src/oship/atbldr
> 
> the code above returns: 
> /home/tim/ref_impl_python/TRUNK/oship/log/at_build_errors.log
> 
> but when I tried a branches version that has the path:
> /home/tim/ref_impl_python/BRANCHES/LNCCWorkshop/oship/src/oship/atbldr
> 
> it SHOULD return:
> /home/tim/ref_impl_python/BRANCHES/LNCCWorkshop/oship/log/at_build_errors.log
>  
> but I get:
> /home/tim/ref_impl_python/BRANCHES/LNCCWork/oship/log/at_build_errors.log
> 
> logfile=os.getcwd()
> print logfile is correct; but when I add the .rstrip('src/oship/atbldr')
> it also strips the 'shop' off of LNCCWorkshop and returns 
> /home/tim/ref_impl_python/BRANCHES/LNCCWork/oship/log/at_build_errors.log
> 
> I had two other people looking at this as we did troubleshooting and we
> could not determine the cause.  It is repeatable with this path name.
> In resolution I renamed the branch to just LNCC and it works fine.
> 
> Thoughts?
> 
> Tim
> 
> 
> 
> 
> 
> 
> 
Always helps to consult documentation when things don't work.

Help on built-in function rstrip:

rstrip(...)
     S.rstrip([chars]) -> string or unicode

     Return a copy of the string S with trailing 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


If you give chars to rstrip() it removes all those characters from the string 
not that substring.

-Larry



More information about the Python-list mailing list