Remove the Extension from a Filename in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# 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.

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')

remove extension from filename

The code for this article is available on GitHub

If you need to split the filename on name and extension, use the following code sample instead.

main.py
import os file_path = '/home/bobbyhadz/Desktop/file.txt' filename, extension = os.path.splitext(file_path) print(filename) # ๐Ÿ‘‰๏ธ '/home/bobbyhadz/Desktop/file' print(extension) # ๐Ÿ‘‰๏ธ '.txt'

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

This is a two-step process:

  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'))

remove extension from filename using pathlib path

The code for this article is available on GitHub

If you need to split the filename on the name and the extension, use the following code sample instead.

main.py
import pathlib file_path = '/home/bobbyhadz/Desktop/file.txt' fpath = pathlib.Path(file_path) print(fpath.suffix) # ๐Ÿ‘‰๏ธ '.txt' print(fpath.suffixes) # ๐Ÿ‘‰๏ธ ['.txt'] print(fpath.stem) # ๐Ÿ‘‰๏ธ 'file' print(fpath.parent) # ๐Ÿ‘‰๏ธ '/home/bobbyhadz/Desktop'

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.

# Remove the extension from a Filename using removesuffix()

Alternatively, you can use the removesuffix() method.

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'

remove extension from filename using removesuffix

The code for this article is available on GitHub

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()

This is a three-step process:

  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'

remove extension from filename using str rsplit

The code for this article is available on GitHub

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 code for this article is available on GitHub

The previously covered approaches are more forgiving in this scenario.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev