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:
print item
if item.endswith('\\') == True:
item = item[0:-1] # This one I googled and
found to remove the last character /
dirListFinal.append(item)
else:
dirListFinal.append(item)
item.endswith() does not seem to be working.
Please help
--
Regrads,
Rajat
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "hello\\"
>>> s.endswith("\\")
True
>>> s[:-1]
'hello'
I hate to say "works for me", but it works for me. Perhaps you should
put some debugging statements in your code to see if that data you are
working on is what you are expecting.
Try something like this:
>>> x = 'test\\'
>>> if x.endswith('\\'):
x = x[:-1]
This works with Python 2.5.2 on Windows XP.
Mike
if item[-1] == '\\':
--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D
> 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:
> print item
> if item.endswith('\\') == True:
explicitly comparing against true is bad style; better write that as
if item.endswith('\\'):
> item = item[0:-1] # This one I googled and
> found to remove the last character /
> dirListFinal.append(item)
> else:
> dirListFinal.append(item)
>
>
> item.endswith() does not seem to be working.
item.endswith("\\") works just fine:
>>> item = "name\\"
>>> print item
name\
>>> print repr(item)
'name\\'
>>> item.endswith("\\")
True
>>> if item.endswith("\\") == True:
... print item[:-1]
...
name
instead of assuming that some builtin function is broken, try printing
the the value of item to check that it really contains what you think it
does. (use print repr(item) to see control characters etc).
</F>
There is just a single \ at the end of every item. My list is as below:
['Results v1.0/', 'Results v1.1/']
so,
if x.endswith('\\'):
is that correct?
Sorry Guys. I did rubbish here and bothered you guys.
I did not recognize that I need to check for / character and not \
Really very sorry. Working for the last 14 hrs, could not see this
properly. Its time I must go home and take rest.
Regards,
Rajat
> There is just a single \ at the end of every item. My list is as below:
> ['Results v1.0/', 'Results v1.1/']
>
> so,
>
> if x.endswith('\\'):
>
> is that correct?
if your list contains forward slashes, maybe you should test for forward
slashes and not backward slashes.
</F>
> On Fri, Aug 29, 2008 at 07:28:40PM +0100, dudeja...@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
--
Regrads,
Rajat
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))