How to delete a last character from a string

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Aug 29 15:37:50 EDT 2008


On Fri, 29 Aug 2008 14:46:53 -0400, Derek Martin wrote:

> On Fri, Aug 29, 2008 at 07:28:40PM +0100, dudeja.rajat at 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



More information about the Python-list mailing list