473,659 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stripping parts of a path

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.getc wd().rstrip('sr c/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.getc wd()
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/...loper%27s+page
*************** *************** *************** *************** **************

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

iD8DBQBIixGT2TF RV0OoZwMRAkxqAJ 9mGiYAsnhdGyTpS s90vwm6JYqKBQCg mvLh
xLE9S3KUB/aTcG5QvP3T1jg=
=IB+1
-----END PGP SIGNATURE-----

Jul 26 '08 #1
4 1414
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.getc wd().rstrip('sr c/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.getc wd()
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
Jul 26 '08 #2
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.getc wd().rstrip('sr c/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.getc wd()
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.

Jul 26 '08 #3
Tim Cook <ti************ ***@gmail.comwr ote:
>
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.get cwd().rstrip('s rc/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, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 27 '08 #4

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/...loper%27s+page
*************** *************** *************** *************** **************

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

iD8DBQBIjDCA2TF RV0OoZwMRAnNMAJ 9XM8o6A9EOTi1J3 I88gvp9WjYqVQCg mSTo
S6oUOvgps+5rsL5 QtanW5BE=
=wmzo
-----END PGP SIGNATURE-----

Jul 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3329
by: joram gemma | last post by:
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\
258
8591
by: Terry Andersen | last post by:
If I have: struct one_{ unsigned int one_1; unsigned short one_2; unsigned short one_3; }; struct two_{ unsigned int two_1;
6
3545
by: jojobar | last post by:
Hello, I look at the asp.net 2.0 web parts tutorial on the asp.net web site. I tried to run it under firefox browser but it did not run. If I want to use this feature in a commercial product where the user can run on firefox/mozilla, what would be a good approach. 1. Should I overwrite the javascript code drag-and-drop to make it more browser independent. If I want to go this route, can anybody provide me a
7
2283
by: Edward Elliott | last post by:
I'm looking for the "best" way to strip a large set of chars from a filename string (my definition of best usually means succinct and readable). I only want to allow alphanumeric chars, dashes, and periods. This is what I would write in Perl (bless me father, for I have sinned...): $filename =~ tr/\w.-//cd, or equivalently $filename =~ s/// I could just use re.sub like the second example, but that's a bit overkill. I'm trying to...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.