473,503 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: How to delete a last character from a string

On Fri, Aug 29, 2008 at 07:28:40PM +0100, du**********@gmail.com wrote:
dirListFinal = []
for item in dirList:
print item
if item.endswith('\\') == True:
if item[-1] == '\\':

--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFIuEQddjdlQoHP510RAvf3AJ9c9UtXjSqHbSpCYzCCS4 1tPHJEWQCfYC0K
ZQPuVibljLjCufrj5FKKHqU=
=8sCR
-----END PGP SIGNATURE-----

Aug 29 '08 #1
3 1408
On Fri, 29 Aug 2008 14:46:53 -0400, Derek Martin wrote:
On Fri, Aug 29, 2008 at 07:28:40PM +0100, du**********@gmail.com wrote:
>dirListFinal = []
for item in dirList:
print item
if item.endswith('\\') == True:

if item[-1] == '\\':
Which will fail badly if item is the empty string.
Rajat, rather than trying to invent your own code to join directory
paths, you should use the os.path module:
>>import os
os.path.join('/dir', 'x', 'y', 'z', 'file.txt')
'/dir/x/y/z/file.txt'
>>os.path.split('/a/b/c/d/file.txt')
('/a/b/c/d', 'file.txt')
--
Steven
Aug 29 '08 #2
On Fri, Aug 29, 2008 at 8:37 PM, Steven D'Aprano
<st***@remove-this-cybersource.com.auwrote:
On Fri, 29 Aug 2008 14:46:53 -0400, Derek Martin wrote:
>On Fri, Aug 29, 2008 at 07:28:40PM +0100, du**********@gmail.com wrote:
>>dirListFinal = []
for item in dirList:
print item
if item.endswith('\\') == True:

if item[-1] == '\\':

Which will fail badly if item is the empty string.
Rajat, rather than trying to invent your own code to join directory
paths, you should use the os.path module:
>>>import os
os.path.join('/dir', 'x', 'y', 'z', 'file.txt')
'/dir/x/y/z/file.txt'
>>>os.path.split('/a/b/c/d/file.txt')
('/a/b/c/d', 'file.txt')
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
Steven, Thanks so much.
I just found out that I unknowingly searching for something else i.e.
/ rather than \
Just corrected this mistake and fixed it.
--
Regrads,
Rajat
Aug 29 '08 #3
On Fri, Aug 29, 2008 at 07:37:50PM +0000, Steven D'Aprano wrote:
On Fri, 29 Aug 2008 14:46:53 -0400, Derek Martin wrote:
On Fri, Aug 29, 2008 at 07:28:40PM +0100, du**********@gmail.com wrote:
dirListFinal = []
for item in dirList:
print item
if item.endswith('\\') == True:
if item[-1] == '\\':
Which will fail badly if item is the empty string.
Sure. I made 2 assumptions:

1. The OP didn't say how endswith() was failing. My assumption was it
was missing from his version of python (i.e. he's using a version that
predates the feature). I have no idea if that's possible or not...
I've used python 2.4 almost exclusively. I've run into a number of
cases where some clever trick I found on-line didn't work because it
was new in Python 2.5 (or whatever), so it was easy for me to lazily
assume that it could be the case here.

2. If his particular use case didn't exclude the possibility of an
empty string, he'd be smart enough to check that first, or wrap it in
a try block.
Rajat, rather than trying to invent your own code to join directory
paths, you should use the os.path module:
>import os
os.path.join('/dir', 'x', 'y', 'z', 'file.txt')
I agree completely, though I do find that there's one thing missing
from this module's support, which your example illustrates nicely: a
way to find out what the root of the current filesystem is (for some
definition of current which I will let the reader figure out). You
explicitly wrote '/dir', which would likely be wrong on non-Unix OSes,
though IIRC it might work anyway, depending on your particular
environment (e.g. cygwin).

I usually end up doing this via the following function:

def _get_path_root(path):
'''recursive algorithm to determine the name of the root of the file
system in (hopefully) an os-independent way'''

if os.path.dirname(path) == path:
return path
return _get_path_root(os.path.dirname(path))

You can, depending on your needs, either pass it the absolute path of
your script, or the absolute path of your data file(s). For the sake
of performance, I get the real absoulte path outside function:

x = _get_path_root(os.path.realpath(foo))

--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFIuGAIdjdlQoHP510RAnN9AKCbcmYeJOu8hEYL1hnDFX CMpZ451wCggWEc
uSuUXyErOdNV7kVGSZU+US8=
=lZDS
-----END PGP SIGNATURE-----

Aug 29 '08 #4

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

Similar topics

1
36263
by: Olivia Towery | last post by:
How do I delete the last character in a string? Thanks, Olivia Towery
6
2255
by: Carol | last post by:
With ASP, I am generating a huge CSV file. It is to large to open in notepad. Can I use the Filesystem Scripting Object to read the very last line of the file and delete the very last...
3
3995
by: ozbear | last post by:
The title says it all. Since a Richedit control doesn't understand a backspace character ('\b', or 0x08) when I receive one I need to programmatically delete the last character from the Richedit...
5
1751
by: michael | last post by:
Hi All, I have the following: const int LENGTH = 5; void limitNameLength(string inText, char *&outText, int outLength){ if(static_cast<int>(inText.length()) outLength){ inText =...
7
6905
by: ITAutobot25 | last post by:
My delete button is not working in my GUI and my due date is today before midnight. Can anyone show me how to correct this error? My assignment statement is below as well as 5 classes. InventoryGUI...
2
3068
by: Joca | last post by:
This is probably something easy to do, but i'm trying to erase the last character from a string, for example: "Cats" into "Cat" Does anyone have any idea on how to do that? Thanks in advance.
0
779
by: dudeja.rajat | last post by:
Hi, I've a list some of whose elements with character \. I want to delete this last character from the elements that have this character set at their end, I have written a small program,...
5
15582
by: dudeja.rajat | last post by:
Sorry : Earlier mail had a typo in Subject line which might look in-appropriate to my friends Hi, I've a list some of whose elements with character \. I want to delete this last character...
0
153
by: dudeja.rajat | last post by:
On Fri, Aug 29, 2008 at 7:40 PM, Chris Rebert <cvrebert+clp@gmail.comwrote: Thanks for the suggestions. I wondered if the item is really a string. So I added the following to check this: ...
0
1122
by: Fredrik Lundh | last post by:
dudeja.rajat@gmail.com wrote: explicitly comparing against true is bad style; better write that as if item.endswith('\\'): item.endswith("\\") works just fine:
0
7199
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,...
0
7274
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,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6984
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...
0
7453
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...
0
4670
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...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
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...

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.