Last updated: Apr 9, 2024
Reading timeยท4 min
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.
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')
If you need to split the filename on name and extension, use the following code sample instead.
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.
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.
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.
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.
This is a two-step process:
pathlib.Path()
class to create a Path object.with_suffix()
method to remove the extension from the filename.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'))
If you need to split the filename on the name and the extension, use the following code sample instead.
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.
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.
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.
# ๐๏ธ '/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.
This is a three-step process:
str.rsplit()
method to split the filename on a period, once, from
the right.0
.0
will contain the filename without the extension.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.
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:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At 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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: