HOWTO · Python
How to Python Get Filename Without Extension From Path
This tutorial demonstrates how to get file name without extension from the file path in Python
On this page
To get a filename without its final suffix from a path, use Path(file_path).stem. For example, the path Desktop/folder/myfile.txt has the stem myfile. Path.stem is a property, so access it without parentheses. The examples below use a path string and print the resulting name.
Get a filename without its final suffix with Path.stem
pathlib.Path handles the path separators for the current platform, so you do not need to split the path yourself. Construct a Path from the string, then read its stem property. This removes the final suffix while keeping earlier dots in the filename.
Run this example:
from pathlib import Path
file_path = "Desktop/folder/myfile.txt"
file_name = Path(file_path).stem
print(file_name)
myfile
Use os.path.splitext() to remove the final suffix
The standard-library function os.path.splitext() returns a pair: the path portion before the final suffix and the suffix itself. Select the first item to discard that suffix. If you also need only the final path component, splitting on / works for this example, but it is tied to that separator and is less portable across operating systems.
Run this example:
import os
file_path = "Desktop/folder/myfile.txt"
path_without_suffix = os.path.splitext(file_path)[0]
file_name = path_without_suffix.split("/")[-1]
print(file_name)
myfile
Extract the basename before removing the suffix
For a path-aware os.path solution, first call os.path.basename() to get the final component, then pass that result to os.path.splitext(). The basename of Desktop/folder/myfile.txt is myfile.txt; removing its final suffix gives myfile. This pattern avoids manually choosing a path separator.
Run this example:
import os
file_path = "Desktop/folder/myfile.txt"
basename = os.path.basename(file_path)
file_name = os.path.splitext(basename)[0]
print(file_name)
myfile
Handle compound suffixes and dots in filenames
These standard approaches remove one final suffix, not every dot-separated part. For myfile.tar.gz, both Path.stem and os.path.splitext() produce myfile.tar. That is useful when only .gz is the suffix. If the application treats .tar.gz as one compound suffix, handle that known suffix explicitly, for example with name.removesuffix(".tar.gz"). str.removesuffix() was added in Python 3.9, so this expression requires Python 3.9 or later. On older versions, use name[:-len(".tar.gz")] if name.endswith(".tar.gz") else name to remove that exact ending safely.
A name without a suffix, such as README, stays README with Path.stem and os.path.splitext(). A leading-dot name such as .bashrc is treated as a dotfile without a suffix: Path(".bashrc").stem and os.path.splitext(".bashrc")[0] both return .bashrc. By contrast, archive. has a trailing dot: Path.stem preserves it as archive., while os.path.splitext() treats the dot as a suffix and returns archive.
Splitting at the first dot does something different: it truncates the rest of the name. For my.file.tar.gz, first-dot splitting yields my, while removing only the final suffix yields my.file.tar. Choose based on whether the task is to remove a known compound suffix, just the last suffix, or everything from the first dot. First-dot splitting also turns .bashrc into an empty string, so it is unsuitable when leading-dot names should be preserved.
Run this example:
from pathlib import Path
for name in ("myfile.tar.gz", "my.file.tar.gz"):
print(Path(name).stem)
print(name.split(".", 1)[0])
myfile.tar
myfile
my.file.tar
my