removing extension

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sun Apr 27 12:40:41 EDT 2008


On Sun, 27 Apr 2008 15:06:54 +0000, Matt Nordhoff wrote:

> Arnaud Delobelle wrote:
>> More simply, use the rsplit() method of strings:
>> 
>>>>> path = r'C:\myimages\imageone.jpg'
>>>>> path.rsplit('.', 1)
>> ['C:\\myimages\\imageone', 'jpg']
>> 
>> 
>>>>> path = r"C:\blahblah.blah\images.20.jpg"
>>>>> path.rsplit('.', 1)
>> ['C:\\blahblah.blah\\images.20', 'jpg']
>> 
>> HTH
> 
> There's os.path.splitext(), which probably does just about exactly that.

Not exactly.  In the case of no extension `os.path.splitext()` still works:

In [14]: 'foo/bar.txt'.rsplit('.')
Out[14]: ['foo/bar', 'txt']

In [15]: 'foo/bar'.rsplit('.')
Out[15]: ['foo/bar']

In [16]: os.path.splitext('foo/bar')
Out[16]: ('foo/bar', '')

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list