Extract the extension from a filename in Python

avatar

Borislav Hadzhiev

Last updated: Aug 19, 2022

banner

Photo from Unsplash

Extract the extension from a filename in Python #

Use the os.path.splitext() method to extract the extension from a filename, e.g. extension = os.path.splitext(file_path)[1]. 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/example.pdf' filename, extension = os.path.splitext(file_path) print(filename) # 👉️ '/home/bobbyhadz/Desktop/example' print(extension) # 👉️ '.pdf' extension = os.path.splitext(file_path)[1] print(extension) # 👉️ '.pdf'

We used the os.path.splitext method to extract 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/example.pdf' # 👇️ ('/home/bobbyhadz/Desktop/example', '.pdf') print(os.path.splitext(file_path)) filename, extension = os.path.splitext(file_path) print(filename) # 👉️ '/home/bobbyhadz/Desktop/example' print(extension) # 👉️ '.pdf'

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

Previous periods are ignored if the path contains multiple.

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

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

Extract the extension from a filename using pathlib.Path #

To extract the extension from a filename:

  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. The suffix attribute returns the file extension if the file contains one.
main.py
import pathlib file_path = '/home/bobbyhadz/Desktop/example.pdf' fpath = pathlib.Path(file_path) extension = fpath.suffix print(extension) # 👉️ '.pdf' extensions = fpath.suffixes print(extensions) # 👉️ ['.pdf'] filename = fpath.stem print(filename) # 👉️ 'example' parent = fpath.parent print(parent) # 👉️ '/home/bobbyhadz/Desktop'

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

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/example.tar.gz' fpath = pathlib.Path(file_path) extension = fpath.suffix print(extension) # 👉️ '.gz' extensions = fpath.suffixes print(extensions) # 👉️ ['.tar', '.gz'] filename = fpath.stem print(filename) # 👉️ 'example.tar' parent = fpath.parent print(parent) # 👉️ '/home/bobbyhadz/Desktop'

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

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.