This post will discuss how to get a filename without an extension from the specified path in Python.

1. Using os.path.splitext() function

The standard solution is to use the os.path.splitext(path) function to split a path into a (root, ext) pair such that root + ext == path. This returns the path to the file without extension. If the file has multiple periods, leading periods are ignored.

Download Code

2. Using str.rsplit() function

Alternatively, you can use the str.rsplit() function to split on the last period.

Download Code

3. Using pathlib.Path.stem() function

If you don’t need the complete path, you can use the pathlib module in Python 3.4+. You can use its stem property that returns the file name without its extension:

Download Code

That’s all about getting filename without extension in Python.