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 "
Thought I would throw in a variation to the use of the os.path.splitext without the need to use array indexing.
The function always returns a (root, ext)
pair so it is safe to use:
root, ext = os.path.splitext(path)
Example:
>>> import os
>>> path = 'my_text_file.txt'
>>> root, ext = os.path.splitext(path)
>>> root
'my_text_file'
>>> ext
'.txt'