473,657 Members | 2,832 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**********@gm ail.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)

iD8DBQFIuEQddjd lQoHP510RAvf3AJ 9c9UtXjSqHbSpCY zCCS41tPHJEWQCf YC0K
ZQPuVibljLjCufr j5FKKHqU=
=8sCR
-----END PGP SIGNATURE-----

Aug 29 '08 #1
3 1419
On Fri, 29 Aug 2008 14:46:53 -0400, Derek Martin wrote:
On Fri, Aug 29, 2008 at 07:28:40PM +0100, du**********@gm ail.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**********@gm ail.com wrote:
>>dirListFina l = []
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.spli t('/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**********@gm ail.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.realpat h(foo))

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

iD8DBQFIuGAIdjd lQoHP510RAnN9AK CbcmYeJOu8hEYL1 hnDFXCMpZ451wCg gWEc
uSuUXyErOdNV7kV GSZU+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
36478
by: Olivia Towery | last post by:
How do I delete the last character in a string? Thanks, Olivia Towery
6
2267
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 character? I am hoping the DSO will not have to read thru the whole file. it is probably way to big. Thank youi very much
3
4012
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 control. This is a WinForms application, not a webform. I have tried... re.SelectionLength = 1; re.SelectionStart = re.Textlength-1; re.SelectionText = ""; // empty string
5
1759
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 = inText.substr(0, outLength); }
7
6926
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 is the main class to begin execution. Thanks! • Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the...
2
3080
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
794
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, unfortunately this does not work: dirListFinal = for item in dirList:
5
15594
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 from the elements that have this character set at their end,
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: item = "" for item in dirList: print type(item)
0
1134
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
8385
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
8821
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...
0
8602
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...
0
7316
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
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();...
0
4150
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.