How to get the filename without the extension from a path in Python?
For instance, if I had "/path/to/some/file.txt"
, I would want "
You can make your own with:
>>> import os
>>> base=os.path.basename('/root/dir/sub/file.ext')
>>> base
'file.ext'
>>> os.path.splitext(base)
('file', '.ext')
>>> os.path.splitext(base)[0]
'file'
Important note: If there is more than one .
in the filename, only the last one is removed. For example:
/root/dir/sub/file.ext.zip -> file.ext
/root/dir/sub/file.ext.tar.gz -> file.ext.tar
See below for other answers that address that.