FelixLeg
(Przemysław Chojnacki)
1
This is my most strange bug I have ever meet…
when I run the below code in python interpreter:
>>> x = "abc.txt"
>>> x.rstrip(".txt")
'abc'
It works as expected. However in my test code:
filename = existing_source.name
print(f"\t 1: {filename}/{type(filename)} {self.source_suffix}/{type(self.source_suffix)}")
filename = filename.rstrip(self.source_suffix)
print(f"\t 2: {filename}")
filename += self.target_suffix
print(f"\t 3: {filename}")
prints this:
1: test.txt/<class 'str'> .txt/<class 'str'>
2: tes
3: tes.md
I really don’t know what is going on
Why it “swallowed” one “t” in “test”?
bschubert
(Brian Schubert)
2
>>> x = "abc.txt"
>>> x.rstrip("t.x")
'abc'
P.S. using a linter like ruff or flake8 can help catch hiccups like this:
https://docs.astral.sh/ruff/rules/bad-str-strip-call/
https://docs.astral.sh/ruff/rules/strip-with-multi-characters/
FelixLeg
(Przemysław Chojnacki)
3
Thanks! Now I feel so greenhorn now
I always pointed others to a documentation, but this time it was me that forgot this.
MRAB
(Matthew Barnett)
4
Since Python 3.9 there’s the .removesuffix method which does what you want.
I have also seeing strage similar behaviour in Python3.10
>>> "temp.py".rstrip(".py")
'tem'
>>> "tempa.py".rstrip(".py")
'tempa'
>>> "tempa.py".rstrip(".apy")
'tem'
>>> "tempa.py".rstrip(".py")
'tempa'
Ic ok missed the docs Built-in Types — Python 3.10.17 documentation, its mention there 
also noticed its mention in thread above .removesuffix.