Stripping parts of a path
kmtracey at gmail.com
Sat Jul 26 15:48:14 CEST 2008
On Sat, Jul 26, 2008 at 7:59 AM, Tim Cook <timothywayne.cook at gmail.com>wrote:
> Hi All,
> I just ran into an issue with the rstrip method when using it on path
> 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.
> this worked when the path was:
> the code above returns:
> but when I tried a branches version that has the path:
> it SHOULD return:
> but I get:
> print logfile is correct; but when I add the .rstrip('src/oship/atbldr')
> it also strips the 'shop' off of LNCCWorkshop and returns
> 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.
I think rstrip does not do what you think it does. From:
"The chars argument is not a suffix; rather, all combinations of its values
Thus, since the characters p,o,h, and s are all in the argument you supplied
to rstrip, they are removed, then stripping is stopped at the k because that
is not in the chars arg you supplied.
To do what you really want I think you need to use rfind/rindex to locate
the last occurrence of the substring and then truncate the string yourself.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list