How do I get the filename without the extension from a path in Python?

To extract the filename without the extension from a path in Python, you can use either the os.path module (for legacy code) or the modern pathlib module. Here’s how to do it:

Method 1: Using pathlib (Python 3.4+)

from pathlib import Path

file_path = "/path/to/your/file.txt"
filename = Path(file_path).stem  # Extracts "file" from "file.txt"

print(filename)  # Output: "file"

Example with multiple extensions:

file_path = "/path/to/archive.tar.gz"
filename = Path(file_path).stem  # Extracts "archive.tar" (removes last extension)
print(filename)  # Output: "archive.tar"

Method 2: Using os.path (Legacy Approach)

import os

file_path = "/path/to/your/file.txt"
base_name = os.path.basename(file_path)  # Extracts "file.txt"
filename = os.path.splitext(base_name)[0]  # Splits into ("file", ".txt")

print(filename)  # Output: "file"

Example with multiple extensions:

file_path = "/path/to/archive.tar.gz"
base_name = os.path.basename(file_path)  # "archive.tar.gz"
filename = os.path.splitext(base_name)[0]  # ("archive.tar", ".gz") → "archive.tar"
print(filename)

Key Notes

  • Removes Only the Last Extension
    Both methods strip the last extension (e.g., .txt in file.txt, .gz in file.tar.gz).
  • Handles Edge Cases:
  • Paths with no extension: /path/to/READMEREADME.
  • Hidden files (Unix): .bashrc.bashrc (no extension removed).
  • Paths with directories: /path/to/file.txtfile.

Remove All Extensions (Optional)

If you want to remove all extensions (e.g., file.tar.gzfile), split iteratively:

from pathlib import Path

file_path = "/path/to/archive.tar.gz"
filename = Path(file_path).stem  # "archive.tar"
while filename != (filename := Path(filename).stem):
    pass

print(filename)  # Output: "archive"

Comparison

MethodProsCons
pathlibModern, readable, OOP-basedRequires Python 3.4+
os.pathBackward-compatibleLess intuitive for new code

Final Recommendation

Use pathlib for new code—it’s cleaner and handles paths more elegantly. For legacy code, stick with os.path.

Leave a Reply

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