How to Get File Extension in Python Easily
In Python, you can get a file extension using
os.path.splitext() which splits the filename and extension, or by using pathlib.Path.suffix for a modern approach. Both methods return the extension including the dot, like ".txt".Syntax
There are two common ways to get a file extension in Python:
- Using
os.path.splitext(path): This function splits the file path into a tuple (root, ext), whereextis the extension including the dot. - Using
pathlib.Path(path).suffix: This property returns the file extension as a string, including the dot.
python
import os from pathlib import Path # Using os.path.splitext root, ext = os.path.splitext('example.txt') # Using pathlib.Path ext_pathlib = Path('example.txt').suffix
Example
This example shows how to get the file extension from a filename using both os.path.splitext and pathlib.Path.suffix. It prints the extension including the dot.
python
import os from pathlib import Path filename = 'report.pdf' # Using os.path.splitext _, extension1 = os.path.splitext(filename) print('Extension using os.path.splitext:', extension1) # Using pathlib.Path extension2 = Path(filename).suffix print('Extension using pathlib.Path.suffix:', extension2)
Output
Extension using os.path.splitext: .pdf
Extension using pathlib.Path.suffix: .pdf
Common Pitfalls
Some common mistakes when getting file extensions include:
- Not handling files without extensions, which return an empty string.
- Using string methods like
split('.')[-1]which can fail if the filename has multiple dots or no extension. - For hidden files on Unix systems (like
.bashrc),os.path.splitexttreats the whole name as extension.
Always prefer os.path.splitext or pathlib.Path.suffix for reliable results.
python
filename = 'archive.tar.gz' # Wrong way: splitting by dot ext_wrong = filename.split('.')[-1] print('Wrong extension:', ext_wrong) # Outputs 'gz' only # Right way: using os.path.splitext twice import os root, ext = os.path.splitext(filename) root2, ext2 = os.path.splitext(root) print('Correct extensions:', ext2 + ext) # Outputs '.tar.gz'
Output
Wrong extension: gz
Correct extensions: .tar.gz
Quick Reference
| Method | Description | Returns |
|---|---|---|
| os.path.splitext(path) | Splits path into root and extension | Tuple (root, extension with dot) |
| pathlib.Path(path).suffix | Gets file extension as string | Extension string with dot |
| pathlib.Path(path).suffixes | Gets all extensions for multi-dot files | List of extensions with dots |
Key Takeaways
Use os.path.splitext() or pathlib.Path.suffix to get file extensions safely.
File extensions include the dot, e.g., '.txt'.
Avoid splitting filenames by dot manually to prevent errors.
For files with multiple extensions, use pathlib.Path.suffixes.
Files without extensions return an empty string as extension.