Split filename on name and extension in Python

avatar

Borislav Hadzhiev

Last updated: Aug 19, 2022

banner

Photo from Unsplash

Split filename on name and extension in Python #

Use the os.path.splitext() method to split a filename on the name and extension, e.g. filename, extension = os.path.splitext(file_path). The os.path.splitext() method returns a tuple containing the filename as the first element and the extension as the second.

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 split a filename on the name and extension.

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/file.txt' # 👇️ ('/home/bobbyhadz/Desktop/file', '.txt') print(os.path.splitext(file_path)) filename, extension = os.path.splitext(file_path) print(filename) # 👉️ '/home/bobbyhadz/Desktop/file' print(extension) # 👉️ '.txt'

You can use unpacking to assign the filename and the extension to variables on the same line.

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/file' # 👇️ ('/home/bobbyhadz/Desktop/file', '') print(os.path.splitext(file_path)) filename, extension = os.path.splitext(file_path) print(filename) # 👉️ '/home/bobbyhadz/Desktop/file' print(extension) # 👉️ ''

Previous periods are ignored if the path contains multiple.

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

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

Split filename on name and extension using pathlib.Path #

To split a filename on the name and the extension:

  1. Instantiate the pathlib.Path() class to create a Path object.
  2. Access the suffix attribute on the Path object to get the extension.
  3. Access the stem attribute on the object to get the filename.
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.

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

The suffix attribute returns the file extension if the filename contains one.

The suffixes attribute returns a list of the path's file extensions.

main.py
import pathlib file_path = '/home/bobbyhadz/Desktop/file.tar.gz' fpath = pathlib.Path(file_path) print(fpath.suffix) # 👉️ '.gz' print(fpath.suffixes) # 👉️ ['.tar', '.gz'] print(fpath.stem) # 👉️ 'file.tar' print(fpath.parent) # 👉️ '/home/bobbyhadz/Desktop'

The parent attribute returns the logical parent of the path.

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.