If I have a file name as one of the following:
1.1.1.1.1.jpg 1.1.jpg 1.jpg
How can I get only a file name without extension? Was regular expression appropriate?
In most cases, you should not use regex.
os.path.splitext(filename)[0]
This will also correctly handle the file name, e.g. .bashrc , keeping the entire name.
.bashrc
>>> import os >>> os.path.splitext("1.1.1.1.1.jpg") ('1.1.1.1.1', '.jpg')
If I had to do this with a regex, I would do it like this:
s = re.sub(r'\.jpg$', '', s)
No need for regular expression. os.path.splitext is your friend:
os.path.splitext
os.path.splitext('1.1.1.jpg') >>> ('1.1.1', '.jpg')