Tutorial

How to Get File Extension in Python

Guide to get file extension in Python using os.path.splitext and pathlib.Path.suffix with examples, edge cases, and best practices.

Drake Nguyen

Founder · System Architect

3 min read
How to Get File Extension in Python
How to Get File Extension in Python

Get file extension in Python — quick overview

This guide explains how to get file extension in Python using standard libraries. It compares os.path.splitext and pathlib.Path.suffix/suffixes, shows examples for filenames and full paths, and covers edge cases such as hidden files and multiple extensions (.tar.gz).

Using os.path.splitext

The os.path.splitext function splits a path into a pair (root, ext). It returns the extension including the leading dot or an empty string when no extension is present. This method is widely used to parse a python file extension from a path or filename.

import os

root, ext = os.path.splitext('/home/user/archive.tar.gz')
print(root)  # '/home/user/archive.tar'
print(ext)   # '.gz'

# get file extension without dot
ext_no_dot = ext.lstrip('.')  # 'gz'

# hidden file example
print(os.path.splitext('/home/user/.bashrc'))  # ('/home/user/.bashrc', '')

Using pathlib: suffix and suffixes

pathlib provides an object-oriented approach. Use Path.suffix to get the final extension (with the dot). Use Path.suffixes to retrieve all trailing suffixes, which helps when dealing with compound extensions like .tar.gz.

from pathlib import Path

p = Path('/home/user/archive.tar.gz')
print(p.suffix)    # '.gz' (python get file extension)
print(p.suffixes)  # ['.tar', '.gz']  (useful for multiple extensions)

# get extension without dot
print(p.suffix.lstrip('.'))  # 'gz'

# hidden file
print(Path('/home/user/.bashrc').suffix)  # ''

Choosing between splitext and pathlib

  • Use os.path.splitext when you prefer a simple, cross-Python approach and are already working with os.path utilities (python splitext example).
  • Use pathlib for an object-oriented workflow and when you need suffixes for multiple extensions (python pathlib get file extension, pathlib.Path.suffixes).

Handling common edge cases

  • Hidden files like .bashrc have no extension: both splitext and Path.suffix return an empty extension.
  • Files with multiple dots: os.path.splitext returns only the last suffix, while Path.suffixes returns all parts (helpful for .tar.gz).
  • Directory names with dots are ignored when extracting the file extension — methods operate on the final path component.

Examples: common tasks

  • Get extension from full path: os.path.splitext(path)[1] or Path(path).suffix.
  • Get extension without the leading dot: use .lstrip('.') on results from either method.
  • Get combined extension (e.g., tar.gz): ''.join(Path(path).suffixes).lstrip('.') or join suffixes with a separator.

Practical snippet: return extension consistently

from pathlib import Path

def get_extension(path, keep_dot=False, combine_multiple=False):
    p = Path(path)
    if combine_multiple:
        ext = ''.join(p.suffixes)  # '.tar.gz' or ''
    else:
        ext = p.suffix  # last suffix or ''
    return ext if keep_dot else ext.lstrip('.')

# usage examples
print(get_extension('/tmp/data.csv'))          # 'csv'
print(get_extension('/tmp/archive.tar.gz'))   # 'gz'
print(get_extension('/tmp/archive.tar.gz', combine_multiple=True))  # 'tar.gz'
Tip: For robust filename parsing, prefer pathlib when writing new code; fallback to os.path.splitext when maintaining older scripts.

Conclusion

To get file extension in Python, use os.path.splitext for simple scripts or pathlib.Path.suffix and suffixes for an object-oriented approach and better handling of multiple extensions. Handle hidden files and multiple dots explicitly depending on your needs to ensure correct python filename extension extraction.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.