removing extension

Arnaud Delobelle arnodel at googlemail.com
Sun Apr 27 13:51:50 CEST 2008


Lie <Lie.1296 at gmail.com> writes:

> On Apr 27, 6:05 pm, Lie <Lie.1... at gmail.com> wrote:
>>
>> I don't know if this is the simplest way, but you can use re module.
>>
>> import re
>> pat = re.compile(r'(.*?)\..*')
>
> Sorry, this line should be:
> pat = re.compile(r'(.*)\..*')
>
> or paths like these wouldn't pass correctly:
> "C:\\blahblah.blah\\images.20.jpg"
>
>> name = pat.search('C:\\myimages\\imageone.jpg').group(1)
>> print name

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

-- 
Arnaud



More information about the Python-list mailing list