Stripping parts of a path

T

Tim Cook

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's+page
**************************************************************************

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBIixGT2TFRV0OoZwMRAkxqAJ9mGiYAsnhdGyTpSs90vwm6JYqKBQCgmvLh
xLE9S3KUB/aTcG5QvP3T1jg=
=IB+1
-----END PGP SIGNATURE-----
 
D

Dan Stromberg

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

Tim Roberts

Tim Cook said:
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)]
 
T

Tim Cook

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's+page
**************************************************************************

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBIjDCA2TFRV0OoZwMRAnNMAJ9XM8o6A9EOTi1J3I88gvp9WjYqVQCgmSTo
S6oUOvgps+5rsL5QtanW5BE=
=wmzo
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top