SysTutorials

Extracting File Extensions in Python with pathlib and os.path

· updated April 12, 2026

The most reliable way to get a file extension in Python is os.path.splitext(), which splits a filename into the base name and extension. For modern code, pathlib.Path provides a cleaner alternative.

Using os.path.splitext()

os.path.splitext() returns a tuple of (name, extension):

import os

name, ext = os.path.splitext('file.txt')
print(name)  # 'file'
print(ext)   # '.txt'

This works with full paths too:

name, ext = os.path.splitext('/home/user/documents/report.pdf')
print(name)  # '/home/user/documents/report'
print(ext)   # '.pdf'

Using pathlib (Modern Approach)

For Python 3.4+, pathlib.Path is the preferred approach. It’s more object-oriented and platform-agnostic:

from pathlib import Path

file_path = Path('file.txt')
extension = file_path.suffix
print(extension)  # '.txt'

# Get name without extension
stem = file_path.stem
print(stem)  # 'file'

With full paths:

from pathlib import Path

file_path = Path('/home/user/documents/report.pdf')
print(file_path.suffix)  # '.pdf'
print(file_path.stem)    # 'report'

Handling Edge Cases

Multiple dots in filename

Both methods handle files with multiple dots correctly:

import os
from pathlib import Path

filename = 'archive.tar.gz'

# os.path.splitext
_, ext = os.path.splitext(filename)
print(ext)  # '.gz' (only the last extension)

# pathlib
print(Path(filename).suffix)  # '.gz'

If you need to extract .tar.gz as a unit, you’ll need custom logic:

from pathlib import Path

filename = 'archive.tar.gz'
suffixes = Path(filename).suffixes
print(suffixes)  # ['.tar', '.gz']

# Join them back if needed
full_ext = ''.join(suffixes)
print(full_ext)  # '.tar.gz'

Files with no extension

Both methods handle extensionless files gracefully:

import os
from pathlib import Path

filename = 'README'

# os.path.splitext
name, ext = os.path.splitext(filename)
print(name, ext)  # 'README' ''

# pathlib
print(Path(filename).suffix)  # ''
print(Path(filename).stem)    # 'README'

Hidden files (Unix/Linux)

Files starting with a dot (like .bashrc) are treated as having no extension:

from pathlib import Path

print(Path('.bashrc').suffix)  # ''
print(Path('.bashrc').stem)    # '.bashrc'

Comparison

Approach Pros Cons
os.path.splitext() Works in all Python versions, handles paths Less intuitive, string-based
pathlib.Path Modern, object-oriented, cleaner syntax Requires Python 3.4+

Recommendation

Use pathlib.Path for new code targeting Python 3.6+. It’s more readable, safer for cross-platform work, and the standard library approach. Use os.path.splitext() if you need compatibility with older Python versions or are already working heavily with the os module.

# Modern style
from pathlib import Path

ext = Path(filename).suffix
name = Path(filename).stem

Quick Reference

This article covered the essential concepts and commands for the topic. For more information, consult the official documentation or manual pages. The key takeaway is to understand the fundamentals before applying advanced configurations.

Practice in a test environment before making changes on production systems. Keep notes of what works and what does not for future reference.

2026 Best Practices and Advanced Techniques

For Extracting File Extensions in Python with pathlib and os.path, understanding both the fundamentals and modern practices ensures you can work efficiently and avoid common pitfalls. This guide extends the core article with practical advice for 2026 workflows.

Troubleshooting and Debugging

When issues arise, a systematic approach saves time. Start by checking logs for error messages or warnings. Test individual components in isolation before integrating them. Use verbose modes and debug flags to gather more information when standard output is not enough to diagnose the problem.

Performance Optimization

Security Considerations

Security should be built into workflows from the start. Use strong authentication methods, encrypt sensitive data in transit, and follow the principle of least privilege for access controls. Regular security audits and penetration testing help maintain system integrity.

These complementary tools expand your capabilities:

Integration with Modern Workflows

Consider automation and containerization for consistency across environments. Infrastructure as code tools enable reproducible deployments. CI/CD pipelines automate testing and deployment, reducing human error and speeding up delivery cycles.

Quick Reference

This extended guide covers the topic beyond the original article scope. For specialized needs, refer to official documentation or community resources. Practice in test environments before production deployment.

← All posts