Sometimes, we want to get the filename without the extension from a path in Python.
In this article, we’ll look at how to get the filename without the extension from a path in Python.
How to get the filename without the extension from a path in Python?
To get the filename without the extension from a path in Python, we can use the os.path.splitext
method.
For instance, we write
import os
print(os.path.splitext("/path/to/some/file.txt")[0])
to call os.path.splitext
with the path to get the filename for.
Then we get the filename without the extension by getting the first part of the split path string list.
Conclusion
To get the filename without the extension from a path in Python, we can use the os.path.splitext
method.