Skip to main content

How to Remove File Extensions in Python

This guide explores various methods for removing file extensions from filenames in Python. We'll cover the recommended os.path.splitext() and pathlib.Path approaches, along with removesuffix() (Python 3.9+) and the less robust str.rsplit() method, discussing the strengths and weaknesses of each.

The os.path.splitext() function is the standard and most reliable way to split a file path into its base name and extension:

import os

file_path = '/home/tomnolan/Desktop/my-file.txt'
base_name, extension = os.path.splitext(file_path)

print(base_name) # Output: /home/tomnolan/Desktop/my-file
print(extension) # Output: .txt
  • os.path.splitext() returns a tuple containing two strings: (root, ext).
  • root: The path and filename without the extension.
  • ext: The extension, including the leading dot (.). If there's no extension, ext is an empty string.

Removing File Extensions with pathlib.Path

The pathlib module offers an object-oriented approach to file system paths and provides convenient methods for manipulating them.

Using with_suffix('')

The with_suffix('') method replaces the file's suffix with a given value, or removes it completely if an empty string is passed:

import pathlib

file_path = '/home/tomnolan/Desktop/my-file.txt'
path_obj = pathlib.Path(file_path)
result = path_obj.with_suffix('')
print(result) # Output: /home/tomnolan/Desktop/my-file
print(result.with_suffix('.docx')) # Output: /home/tomnolan/Desktop/my-file.docx
  • The with_suffix() will return the same path with a changed suffix.
  • In the example, it is used to remove the suffix completely, or replace it with a different one.

Using the stem Attribute

The stem attribute directly returns the final path component without its suffix:

import pathlib
file_path = '/home/tomnolan/Desktop/my-file.txt'
path_obj = pathlib.Path(file_path)
print(path_obj.stem) # Output: my-file
print(path_obj.suffix) # Output: .txt
print(path_obj.suffixes) # Output: ['.txt']
print(path_obj.parent) # Output: /home/tomnolan/Desktop
  • path_obj.stem extracts only the filename, excluding the extension.
  • path_obj.suffix gives you the file extension (including the leading dot).
  • path_obj.suffixes returns a list of all extensions, useful with multiple extensions like .tar.gz.
  • path_obj.parent gets the directory.

Removing File Extensions with removesuffix() (Python 3.9+)

Python 3.9 introduced the removesuffix() method for strings, which provides a clean way to remove a known suffix:

file_path = '/home/tomnolan/Desktop/my-file.txt'
result = file_path.removesuffix('.txt')
print(result) # Output: /home/tomnolan/Desktop/my-file
print(result + '.docx') # Output: /home/tomnolan/Desktop/my-file.docx
  • removesuffix('.txt') removes the .txt suffix if it's present at the end of the string. If the suffix isn't found, the original string is returned.
  • This is safer and more readable than manual slicing.

While possible, using str.rsplit() is not recommended for removing file extensions. It can lead to incorrect results if the filename itself contains periods.

file_path = '/home/tomnolan/Desktop/my-file.txt'
result = file_path.rsplit('.', 1)[0] # Splits on the LAST period, max 1 time.
print(result) # Output: /home/tomnolan/Desktop/my-file

file_path_no_ext = '/home/tom.nolan/Desktop/my-file'
result = file_path_no_ext.rsplit('.', 1)[0]
print(result) # Output: /home/tom # PROBLEM! Incorrect result
  • rsplit('.', 1) splits the string at most once from the right, using . as the delimiter.
  • [0] selects the first part of the split result.
  • This can cause unexpected behavior if the filename includes a period, but no extensions.
warning

Avoid this method unless you have very specific control over the filename format and are absolutely certain it won't contain periods in the base name.