Remove Literal Python Filename Suffixes with removesuffix
An asset importer turns running.png into an asset ID. Calling rstrip(".png") looks plausible, but produces runni instead of running. The importer has deleted part of the name, creating a risk that unrelated assets receive the same ID.
These examples use Python 3.9 or later and filename strings supplied by a manifest. They transform names in memory without renaming files.
Reproduce the Character Stripping Bug
Run this standalone example:
The argument to rstrip() is a set of removable characters. It keeps removing trailing characters while each belongs to that set. After the extension disappears, it also removes the final g and then n from running. It stops at i, which is outside the set, leaving runni.
Remove One Exact Ending
Literal removal has a narrower contract: removesuffix() removes one complete matching ending, while an absent ending leaves the value unchanged. Matching is case-sensitive. The
Python string reference
documents both removal methods.
This preserves an internal occurrence of .png and removes only the last occurrence when it is at the end. Calling replace(".png", "") would also remove occurrences inside the name, which is inappropriate for this naming rule.
Require the Suffix Before Creating an ID
An optional cleanup operation and a required manifest field need different error policies. Save this helper as asset_id.py when every accepted name must end in lowercase .png and contain a nonempty ID before it:
| |
Run this beside the helper:
The explicit suffix check prevents an unexpected name from passing through unchanged. It does not inspect image contents or validate a filesystem path. If the manifest permits only basenames or a restricted ID alphabet, enforce those additional rules separately before using the ID as a destination path or database key.
Test Names That Ordinary Examples Miss
Save this as test_asset_id.py beside the helper and run python test_asset_id.py:
| |
The png.png case catches a particularly destructive character-stripping mistake: all its characters belong to the stripping set. Repeated endings test the one-removal rule, while uppercase and trailing-space cases verify the manifest’s exact spelling policy. Spaces inside the ID remain intact because this helper does not trim them.
Keep the Removal Rule Explicit
For a generic suffix argument, an empty suffix is a useful boundary test. removesuffix("") preserves the original value, whereas a hand-written slice using [:-len(suffix)] becomes [:-0] and loses everything.
PEP 616
explains the motivation for the dedicated methods and their empty-affix behavior.
The corresponding removeprefix() method handles one literal beginning. Use these methods when the format specifies a known string at a known boundary. Keep rstrip() for removing a set of trailing characters, such as whitespace, when that is the intended operation.
This helper deliberately accepts hero.png.png as the ID hero.png. Reject repeated extensions explicitly if the manifest forbids them. Removing more suffixes in a loop would implement a different naming policy and could merge distinct names. Likewise, preserve case unless the manifest defines a case-insensitive identity rule; an ad hoc lowercase conversion can collapse names that a later consumer treats as distinct.