How can I replace or strip an extension from a filename in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Filename extension changes are simple in Python if you use path-aware tools instead of manual string slicing. The safest choices are os.path.splitext() and pathlib.Path, because they handle common edge cases such as hidden files, multiple dots, and missing extensions much better than split(".").
Replace One Extension Safely
If you want to replace the final extension, os.path.splitext() is the classic tool. It splits a path into two pieces: the base name and the last suffix.
This works well when you want to remove the current suffix and attach a new one. It only removes the final extension, which is exactly what most programs want.
For more modern code, pathlib is usually even clearer:
with_suffix() is the cleanest way to replace one suffix while keeping the rest of the path intact.
Strip an Extension Without Replacing It
If you only need the filename without its final extension, the same utilities work:
Or with os.path:
Both return the name without the last suffix. This is much safer than using a hard-coded slice such as name[:-4], which silently breaks when the extension length changes.
Understand Multi-Suffix Filenames
A name like archive.tar.gz has two suffix-like parts. Both splitext() and Path.stem remove only the final part.
The result of stem is archive.tar, not archive. That is usually correct. In many workflows, .tar.gz is meaningful as a compound extension, and removing only .gz is the right behavior.
If your application truly wants to strip every suffix, make that rule explicit:
Do not do this automatically unless the business rule is clear. Multi-suffix files often need special handling.
Hidden Files and No-Extension Names
Path-aware utilities also behave better for Unix-style hidden files. A leading dot does not automatically mean "extension".
This is one of the biggest reasons not to use split("."). A filename such as .bashrc is a hidden file with no ordinary suffix, not a blank base name plus an extension.
Rename Files on Disk
If you are changing real files instead of just computing new names, pathlib keeps the code compact.
That updates the file on disk. If you only need to generate target paths for later use, call with_suffix() without rename().
For batch operations:
This is clear, readable, and avoids manual filename parsing.
Common Pitfalls
- Using
split(".")and breaking filenames with multiple dots. - Treating hidden files such as
.bashrcas if they had an ordinary extension. - Assuming
stemremoves every suffix instead of only the last one. - Replacing extensions with hard-coded string slices.
- Stripping multiple suffixes without first deciding whether the compound extension is meaningful.
Summary
- Use
os.path.splitext()orpathlib.Pathinstead of manual string splitting. - '
with_suffix()is the cleanest way to replace a single extension.' - '
stemandsplitext()remove only the last suffix.' - Use
suffixesonly when you intentionally need to reason about compound extensions. - Path-aware utilities handle hidden files and no-extension cases correctly.

