Use os.path.splitext:
>>> import os
>>> filename, file_extension = os.path.splitext('/path/to/somefile.ext')
>>> filename
'/path/to/somefile'
>>> file_extension
'.ext'
Unlike most manual string-splitting attempts, os.path.splitext will correctly treat /a/b.c/d as having no extension instead of having extension .c/d, and it will treat .bashrc as having no extension instead of having extension .bashrc:
>>> os.path.splitext('/a/b.c/d')
('/a/b.c/d', '')
>>> os.path.splitext('.bashrc')
('.bashrc', '')
Answer from nosklo on Stack OverflowUse os.path.splitext:
>>> import os
>>> filename, file_extension = os.path.splitext('/path/to/somefile.ext')
>>> filename
'/path/to/somefile'
>>> file_extension
'.ext'
Unlike most manual string-splitting attempts, os.path.splitext will correctly treat /a/b.c/d as having no extension instead of having extension .c/d, and it will treat .bashrc as having no extension instead of having extension .bashrc:
>>> os.path.splitext('/a/b.c/d')
('/a/b.c/d', '')
>>> os.path.splitext('.bashrc')
('.bashrc', '')
New in version 3.4.
import pathlib
print(pathlib.Path('/foo/bar.txt').suffix)
# Outputs: .txt
print(pathlib.Path('/foo/bar.txt').stem)
# Outputs: bar
print(pathlib.Path("hello/foo.bar.tar.gz").suffixes)
# Outputs: ['.bar', '.tar', '.gz']
print(''.join(pathlib.Path("hello/foo.bar.tar.gz").suffixes))
# Outputs: .bar.tar.gz
print(pathlib.Path("hello/foo.bar.tar.gz").stem)
# Outputs: foo.bar.tar
I'm surprised no one has mentioned pathlib yet, pathlib IS awesome!
How to get only the name of the path with python? - Stack Overflow
string - How do I get the filename without the extension from a path in Python? - Stack Overflow
python - Trouble getting file name with extension using pathlib - Stack Overflow
Is there any situation where string.endswith() would not be suitable for checking for a specific file extension?
Videos
In Python 3.4+, you can use the pathlib module (included in Python's standard library):
>>> from pathlib import Path
>>> p = Path("/home/user/Downloads/repo/test.txt")
>>> print(p.stem)
test
>>> print(p.name)
test.txt
Use the os.path module to work with paths; the os.path.basename() function gives you the last part after the last path separator, and os.path.splitext() gives you the filename with the extension split off:
import os.path
basename = os.path.splitext(os.path.basename(f.name))[0]
Using the os.path functions ensures that your code will continue to work correctly on different operating systems, even if the path separators are different.
In Python 3.4 or newer (or as a separate backport install), you can also use the pathlib library, which offers a more object-oriented approach to path handling. pathlib.Path() objects have a .stem attribute, which is the final component without the extension suffix:
try:
import pathlib
except ImportError:
# older Python version, import the backport instead
import pathlib2 as pathlib
basename = pathlib.Path(f.name).stem
Demo:
>>> import os.path
>>> a = "/home/user/Downloads/repo/test.txt"
>>> os.path.basename(a)
'test.txt'
>>> os.path.splitext(os.path.basename(a))
('test', '.txt')
>>> os.path.splitext(os.path.basename(a))[0]
'test'
>>> import pathlib
>>> pathlib.Path(a)
PosixPath('/home/user/Downloads/repo/test.txt')
>>> pathlib.Path(a).stem
'test'
Python 3.4+
Use pathlib.Path.stem
>>> from pathlib import Path
>>> Path("/path/to/file.txt").stem
'file'
>>> Path("/path/to/file.tar.gz").stem
'file.tar'
Python < 3.4
Use os.path.splitext in combination with os.path.basename:
>>> os.path.splitext(os.path.basename("/path/to/file.txt"))[0]
'file'
>>> os.path.splitext(os.path.basename("/path/to/file.tar.gz"))[0]
'file.tar'
Use .stem from pathlib in Python 3.4+
from pathlib import Path
Path('/root/dir/sub/file.ext').stem
will return
'file'
Note that if your file has multiple extensions .stem will only remove the last extension. For example, Path('file.tar.gz').stem will return 'file.tar'.