To remove one exact trailing string in Python, use name.removesuffix(suffix), not name.rstrip(suffix). The difference matters when preparing filenames, report labels or export identifiers: rstrip treats its argument as a set of removable characters, so it can erase part of the name you intended to preserve. Neither method changes a file on disk; both return a string.
Why rstrip can shorten a valid name
Suppose an export is named report.txt. You want the label report, without the extension. It is tempting to write rstrip('.txt'), but Python does not interpret that argument as an extension. It removes trailing dots, lowercase t characters and lowercase x characters repeatedly, stopping only when a different character is reached. After removing the extension, it also removes the final t in report.
name = 'report.txt'
print(name.rstrip('.txt'))
print(name.removesuffix('.txt'))
The first result is repor; the second is report. This is not a filesystem corruption operation by itself. The risk appears when a later step uses the wrong string to name an output, look up a record or plan a rename. Keep the transformation separate from those side effects so the result can be reviewed first.
Use an exact, case-sensitive suffix rule
str.removesuffix is available in Python 3.9 and later. It removes the supplied nonempty suffix once, and only when the complete string ends with that suffix. If the ending does not match, the value stays unchanged. Matching is case-sensitive: a lowercase rule for .txt does not remove .TXT.
suffix = '.txt'
cases = {
'report.txt': 'report',
'report.csv': 'report.csv',
'report.TXT': 'report.TXT',
'report.txt.txt': 'report.txt',
'.txt': '',
'': '',
}
for original, expected in cases.items():
result = original.removesuffix(suffix)
assert result == expected, (original, result)
print(repr(original), '->', repr(result))
assert 'report.txt'.removesuffix('') == 'report.txt'
This small fixture exposes several policy decisions. Repeated endings are not removed recursively. A name containing only the suffix becomes empty. An empty suffix leaves the input unchanged. If your application cannot accept an empty result, reject it explicitly after transformation rather than assuming the string method validates business rules.
Build a preview before touching files
Start with a short list of actual names copied into a disposable example. Print both original and proposed values, including quotes through repr so whitespace is visible. Review unmatched uppercase endings and unexpected empty results. If a later workflow will write several outputs, also check whether two inputs would produce the same destination: report and report.txt can both become report.
Do not insert a rename call into the demonstration until you have decided how to handle collisions, existing destinations and failures halfway through the operation. A correct suffix transformation is not a guarantee of a safe batch rename. Keep a mapping of original names to proposed names and use a backed-up test directory for any subsequent filesystem experiment.
For compound suffixes such as .tar.gz, passing that entire string removes that exact ending once. That is a text rule, not automatic archive-format detection. If you need path-component or extension semantics, choose a path-handling API deliberately; do not assume every period marks an extension or that everything after the first period should disappear.
Quick checks and common questions
- Should I use rstrip at all? Yes, when trimming a set of trailing characters is genuinely the requirement, such as default whitespace trimming. It is not a substitute for exact suffix removal.
- Does removesuffix modify the original variable? No. Strings are immutable; assign the returned value if you want to keep it.
- Should I lowercase the whole filename first? Not automatically. That also changes the retained name. Define the matching requirement separately from preserving the original spelling.
- Can a successful assertion prove a rename is safe? No. It checks this text behavior, not filesystem permissions, collisions, backups or recovery.
When these names come from configuration, first make sure the input itself is reliable. The related tutorial on rejecting duplicate JSON configuration keys addresses a separate way an intended setting can be lost before the suffix rule even runs.
Source and safety note
The official Python built-in types documentation defines both methods and their different arguments. These examples are for educational purposes. Test carefully before production use. The site owner is not responsible for misuse, damage, data loss or production issues; maintain backups before extending text transformations into file-changing operations.



