Explain Codes LogoExplain Codes Logo

How can I replace (or strip) an extension from a filename in Python?

python
file-handling
pathlib
extensions
Nikita BarsukovbyNikita Barsukov·Aug 10, 2024
TLDR

To quickly strip the extension using Python's os.path.splitext(), simply:

import os name_without_ext = os.path.splitext('example.txt')[0] # Aaaand... It's gone!

The filename is now leaving Extensionville and going to Non-extensionville. It's currently going by the name 'example'.

Easy peasy: Simple usage of os.path.splitext()

Dealing with multiple appendages... I mean, extensions

Some files have a lot of .this.that stuff going on—like a potluck party, but for file extensions. How do you handle these? os.path.splitext is your friend.

import os filename = "archive.tar.gz" while '.' in filename: # Yep, we're going round and round! filename = os.path.splitext(filename)[0] # Now everyone left, and 'archive' is alone.

Ever used SCons? Here's some sugar for you

For the SCons-loving builders among us, os.path.splitext slides in smoothly.

import os env = Environment() filename = 'source.c' # Ssssst.. Password please? name_without_ext = os.path.splitext(filename)[0] # Man of steel? No. Man of Source! object_file = env.Object(target=name_without_ext, source=filename)

Marching with the times: Enter pathlib

pathlib is the new kid on the block—a Python 3.4+ friendly OO-style way to manipulate file paths.

Nothing to see here... Removing extensions with pathlib

from pathlib import Path name_without_ext = Path('example.txt').with_suffix('') # With a flick of the wand

Spicing things up with a new .extension

from pathlib import Path new_name = Path('example.txt').with_suffix('.new_extension') # The shape-shifting spell

Playing god with our own function

from pathlib import Path # Yeah, it's a DIY thing. def replace_ext(filename, new_extension=''): # Behind the veil goes the file, and out comes the new one! return Path(filename).with_suffix(new_extension) new_name = replace_ext('example.txt', '.bak') # A sprinkle of '.bak'

This function is like a bouncer at a club—it only lets the last extension go.

Roadblocks and how to navigate them

Dealing with the extension-less wanderers

import os filename = "README" #Such a lonely file... name, extension = os.path.splitext(filename) # name is 'README', extension is... wait what?

When filenames have identity crises

Our file 'my.file.name.txt' is not sure if it's a period, a dot, a point, or a .

filename = 'my.file.name.txt' name_without_ext = filename.rsplit('.', 1)[0] # 'my.file.name' - Split personality handled!

This only removes the last occurrence and lets the others live happily in Dotland.

Assertions: Your friendly neighborhood fact-checker

assert replace_ext('example.txt', '.bak') == 'example.bak' assert replace_ext('archive.tar.gz', '.bz2') == 'archive.tar.bz2'

Assertions are like your memory capsules. They remember original cases and help your function avoid an identity crisis.