Use Path(path).stem when working with Python path objects, or os.path.splitext(path)[0] when you have a path string. Both remove only the final suffix: for archive.tar.gz, the result is archive.tar. They retain the directory portion unless you also select the basename.
Choose the method that fits your code
| Method | Use it when | Example result for /data/archive.tar.gz |
|---|---|---|
Path(path).stem |
Your code uses pathlib path objects; the result is the final path component without its last suffix. |
archive.tar |
os.path.splitext(path)[0] |
You have a path string and want the path with its last suffix removed. | /data/archive.tar |
Python’s pathlib reference describes pathlib as an object-oriented interface and os.path as a function-based one. It also maps os.path.splitext() to PurePath.stem and PurePath.suffix.
With pathlib
from pathlib import Path
name = Path("/data/archive.tar.gz").stem
print(name) # archive.tar
Path.name gives the final component, Path.stem gives that component without its last suffix, and Path.suffix gives the last suffix. For this example, Path.suffix is .gz.
With os.path
import os
path_without_suffix, suffix = os.path.splitext("/data/archive.tar.gz")
print(path_without_suffix) # /data/archive.tar
print(suffix) # .gz
os.path.splitext() returns a pair: the root and the final extension. Index [0] when you only need the path without that suffix.
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
Get only the filename, not its directory
Path(path).stem already returns the final component, so it excludes parent directories. By contrast, os.path.splitext(path)[0] preserves the directory portion. To get only the filename with its last suffix removed, use either form:
from pathlib import Path
import os
name_with_pathlib = Path("/data/report.csv").stem
name_with_os_path = os.path.basename(os.path.splitext("/data/report.csv")[0])
print(name_with_pathlib) # report
print(name_with_os_path) # report
Understand compound extensions and dotfiles
Compound suffixes
These APIs remove one final suffix, not every dotted part of a name. For archive.tar.gz, Path(...).stem returns archive.tar, while Path(...).suffix returns .gz. To inspect all suffixes, use Path(...).suffixes, which returns ['.tar', '.gz']. If your application treats a compound ending such as .tar.gz as one extension, handle that rule explicitly.
Rank #2
Names beginning with a dot
A leading dot is not necessarily an extension. For example, os.path.splitext(".env") treats .env as the root and returns an empty extension. The official os.path.splitext documentation states that leading periods in the last path component are considered part of the root.
A single dot and Python 3.14
The pathlib documentation notes that a single dot is considered a valid suffix starting in Python 3.14. If your code must run on an earlier Python version, check that version’s documentation before relying on this special case.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Use the right path flavor
Path uses the path conventions of the operating system running the code. If you are processing a Windows path string on a POSIX system, the host’s default Path flavor may not parse it as intended. Use the path flavor appropriate to the path being handled—for example, an explicit Windows path class when parsing Windows-formatted paths on another platform.
Quick Recap
Best Value
Which one should you use?
- Choose
Path(path).stemwhen your surrounding code already usespathliband you want the filename component without its final suffix. - Choose
os.path.splitext(path)when you are working with path strings or need the suffix as a separate value. - Use
Path(path).nameoros.path.basename(...)when you need to strip directory information from a string-based result. - Apply explicit logic if you need to remove a compound suffix rather than just the final one.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




