October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Get a File Name Without Its Extension in Python

Use pathlib’s Path.stem or os.path.splitext() to remove a path’s final suffix in Python. Learn what happens to directories, compound extensions, dotfiles, and cross-platform paths.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Which one should you use?

  • Choose Path(path).stem when your surrounding code already uses pathlib and 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).name or os.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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.