FAQ
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







--
**************************************************************************
Join the OSHIP project. It is the standards based, open source
healthcare application platform in Python.
Home page: https://launchpad.net/oship/
Wiki: http://www.openehr.org/wiki/display/dev/Python+developer%27s+page
**************************************************************************
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-list/attachments/20080726/7548adf3/attachment.pgp>

Search Discussions

  • Karen Tracey at Jul 26, 2008 at 1:48 pm

    On Sat, Jul 26, 2008 at 7:59 AM, 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?
    I think rstrip does not do what you think it does. From:

    http://docs.python.org/lib/string-methods.html

    "The chars argument is not a suffix; rather, all combinations of its values
    are stripped"

    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.

    Karen
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20080726/b4a98b96/attachment.htm>
  • Larry Bates at Jul 26, 2008 at 1:59 pm

    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
  • Dan Stromberg at Jul 26, 2008 at 7:15 pm

    On Sat, 26 Jul 2008 08:59:15 -0300, 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
    Is this the Tim Cook I worked with at UCI?

    Anyway, I think the os.path module might have something for you. As a
    little bonus, it should produce code that works well with other operating
    systems, and should deal gracefully with repeated slashes.
  • Tim Roberts at Jul 27, 2008 at 4:32 am

    Tim Cook wrote:
    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 doesn't do what you think it does. The parameter to rstrip is a set:
    as long as the last character is in the set 'abcdhiloprs/', it will remove
    it and check the next one. All of the characters in "shop" are in that
    set.

    In a few minutes, I couldn't think of a clever one-liner to do this. You
    could do it with re.sub, but that seems like overkill.

    chk = '/src/oship/atbldr'
    cwd = os.getcwd()
    if cwd.endswith( chk ):
    cwd = cwd[:-len(chk)]
    --
    Tim Roberts, timr at probo.com
    Providenza & Boekelheide, Inc.
  • Tim Cook at Jul 27, 2008 at 8:23 am

    On Sun, 2008-07-27 at 04:32 +0000, Tim Roberts wrote:

    This doesn't do what you think it does. The parameter to rstrip is a set:
    as long as the last character is in the set 'abcdhiloprs/', it will remove
    it and check the next one. All of the characters in "shop" are in that
    set.
    Thanks for all the replies.
    You are correct I misunderstood the docs. Finding and slicing works
    great.

    Cheers,
    Tim


    --
    **************************************************************************
    Join the OSHIP project. It is the standards based, open source
    healthcare application platform in Python.
    Home page: https://launchpad.net/oship/
    Wiki: http://www.openehr.org/wiki/display/dev/Python+developer%27s+page
    **************************************************************************
    -------------- next part --------------
    A non-text attachment was scrubbed...
    Name: not available
    Type: application/pgp-signature
    Size: 189 bytes
    Desc: This is a digitally signed message part
    URL: <http://mail.python.org/pipermail/python-list/attachments/20080727/922bc74d/attachment.pgp>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 26, '08 at 11:59a
activeJul 27, '08 at 8:23a
posts6
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase