You're using wrong method. Use str.replace
instead:
>>> "Boat.txt".replace(".txt", "")
'Boat'
NOTE: str.replace
will replace anywhere in the string.
>>> "Boat.txt.txt".replace(".txt", "")
'Boat'
To remove the last trailing .txt
only, you can use regular expression:
>>> import re
>>> re.sub(r".txt$", "", "Boat.txt.txt")
'Boat.txt'
If you want filename without extension, os.path.splitext
is more appropriate:
>>> os.path.splitext("Boat.txt")
('Boat', '.txt')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…