Remove the extension from a Filename in Python

avatar

Borislav Hadzhiev

Last updated: Aug 19, 2022

banner

Photo from Unsplash

Table of Contents #

  1. Remove the extension from a Filename in Python
  2. Remove the extension from a Filename using pathlib.Path
  3. Remove the extension from a Filename using removesuffix()
  4. Remove the extension from a Filename using str.rsplit()

Remove the extension from a Filename in Python #

Use the os.path.splitext() method to remove the extension from a filename, e.g. result = os.path.splitext(file_path)[0]. The os.path.splitext method will return a tuple that contains the filename without the extension as its first element.

main.py
import os file_path = '/home/bobbyhadz/Desktop/my-file.txt' result = os.path.splitext(file_path)[0] # 👇️ '/home/bobbyhadz/Desktop/my-file' print(result) # 👇️ '/home/bobbyhadz/Desktop/my-file.docx' print(result + '.docx')

We used the os.path.splitext method to remove the extension from a filename.

The os.path.splitext method splits the path into a tuple that contains the root and the extension.

main.py
import os file_path = '/home/bobbyhadz/Desktop/my-file.txt' # 👇️ ('/home/bobbyhadz/Desktop/my-file', '.txt') print(os.path.splitext(file_path))

To get the filename without the extension, access the first element in the tuple.

If the specified path doesn't contain an extension, the second element in the tuple is an empty string.

main.py
import os file_path = '/home/bobbyhadz/Desktop/my-file' # 👇️ ('/home/bobbyhadz/Desktop/my-file', '') print(os.path.splitext(file_path))

If the provided path doesn't have an extension, we return the string as is.

Previous periods are ignored if the path contains multiple.

main.py
import os import pathlib file_path = '/home/bobby.hadz/Desktop/my-file.txt' # 👇️ ('/home/bobby.hadz/Desktop/my-file', '.txt') print(os.path.splitext(file_path))

Alternatively, you can use the pathlib.Path() class.

Remove the extension from a Filename using pathlib.Path #

To remove the extension from a filename:

  1. Instantiate the pathlib.Path() class to create a Path object.
  2. Use the with_suffix() method to remove the extension from the filename.
main.py
import pathlib file_path = '/home/bobbyhadz/Desktop/my-file.txt' fpath = pathlib.Path(file_path) result = fpath.with_suffix('') print(result) # 👉️ /home/bobbyhadz/Desktop/my-file print(fpath.stem) # 👉️ 'my-file' # 👇️ '/home/bobbyhadz/Desktop/my-file.docx' print(result.with_suffix('.docx'))

The pathlib.Path class is used to create a PosixPath or a WindowsPath object depending on your operating system.

The with_suffix method takes a suffix and changes the suffix of the path.

We first passed an empty string to the method to remove the extension from the filename.

You can optionally call the method with a different extension if you need to add one.

main.py
import pathlib file_path = '/home/bobbyhadz/Desktop/my-file.txt' fpath = pathlib.Path(file_path) result = fpath.with_suffix('') print(result) # 👉️ /home/bobbyhadz/Desktop/my-file print(fpath.stem) # 👉️ 'my-file' # 👇️ '/home/bobbyhadz/Desktop/my-file.docx' print(result.with_suffix('.docx'))

You can use the stem attribute on the Path object if you need to get only the filename without the extension.

Alternatively, you can use the removesuffix() method.

Remove the extension from a Filename using removesuffix() #

Use the str.removesuffix() method to remove the extension from a filename, e.g. result = file_path.removesuffix('.txt'). The removesuffix() method will remove the specified extension from the filename.

main.py
file_path = '/home/bobbyhadz/Desktop/my-file.txt' result = file_path.removesuffix('.txt') print(result) # 👉️ '/home/bobbyhadz/Desktop/my-file' print(result + '.docx') # 👉️ '/home/bobbyhadz/Desktop/my-file.docx'

The str.removesuffix() method is available in Python 3.9+.

The str.removesuffix method checks if the string ends with the specified suffix and if it does, the method returns a new string excluding the suffix, otherwise it returns a copy of the original string.

main.py
# 👇️ '/home/bobbyhadz/Desktop/my-file' print('/home/bobbyhadz/Desktop/my-file.txt'.removesuffix('.txt')) # 👇️ '/home/bobbyhadz/Desktop/my-file' print('/home/bobbyhadz/Desktop/my-file'.removesuffix('.txt'))

If the string doesn't contain the specified suffix, the method returns the string as is.

Make sure you are running Python version 3.9 or newer to be able to use the str.removesuffix method.

Alternatively, you can use the str.rsplit() method.

Remove the extension from a Filename using str.rsplit() #

To remove the extension from a filename:

  1. Use the str.rsplit() method to split the filename on a period, once, from the right.
  2. Access the list item at index 0.
  3. The list item at index 0 will contain the filename without the extension.
main.py
file_path = '/home/bobbyhadz/Desktop/my-file.txt' result = file_path.rsplit('.', 1)[0] print(result) # 👉️ '/home/bobbyhadz/Desktop/my-file' print(result + '.docx') # 👉️ '/home/bobbyhadz/Desktop/my-file.docx'

The str.rsplit method returns a list of the words in the string using the provided separator as the delimiter string.

main.py
file_path = '/home/bobbyhadz/Desktop/my-file.txt' # 👇️ ['/home/bobbyhadz/Desktop/my-file', 'txt'] print(file_path.rsplit('.', 1))

The method takes the following 2 arguments:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done, the rightmost ones (optional)

Except for splitting from the right, rsplit() behaves like split().

We split the filename, once, on a period, from the right.

This approach handles the scenario where the filename doesn't contain any periods.

main.py
file_path = '/home/bobbyhadz/Desktop/my-file' result = file_path.rsplit('.', 1)[0] print(result) # 👉️ '/home/bobbyhadz/Desktop/my-file'

However, it leads to confusing behavior if the filename contains a period, but doesn't contain an extension.

main.py
file_path = '/home/bobby.hadz/Desktop/my-file' # 👇️ ['/home/bobby', 'hadz/Desktop/my-file'] print(file_path.rsplit('.', 1)) result = file_path.rsplit('.', 1)[0] print(result) # 👉️ '/home/bobby'

The previously covered approaches are more forgiving in this scenario.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.