python: rstrip one exact string, respecting order
Question:
Is it possible to use the python command rstrip
so that it does only remove one exact string and does not take all letters separately?
I was confused when this happened:
>>>"Boat.txt".rstrip(".txt")
>>>'Boa'
What I expected was:
>>>"Boat.txt".rstrip(".txt")
>>>'Boat'
Can I somehow use rstrip and respect the order, so that I get the second outcome?
Answers:
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')
Starting with Python 3.9, use .removesuffix()
:
"Boat.txt".removesuffix(".txt")
On earlier versions of Python, you’ll have to either define it yourself:
def removesuffix(s, suf):
if suf and s.endswith(suf):
return s[:-len(suf)]
return s
(you need to check that suf
isn’t empty, otherwise removing an empty suffix e.g. removesuffix("boat", "")
will do return s[:0]
and return ""
instead of "boat"
)
or use regex:
import re
suffix = ".txt"
s = re.sub(re.escape(suffix) + '$', '', s)
>>> myfile = "file.txt"
>>> t = ""
>>> for i in myfile:
... if i != ".":
... t+=i
... else:
... break
...
>>> t
'file'
>>> # Or You can do this
>>> import collections
>>> d = collections.deque("file.txt")
>>> while True:
... try:
... if "." in t:
... break
... t+=d.popleft()
... except IndexError:
... break
... finally:
... filename = t[:-1]
...
>>> filename
'file'
>>>
This will work regardless of extension type.
# Find the rightmost period character
filename = "my file 1234.txt"
file_extension_position = filename.rindex(".")
# Substring the filename from first char up until the final period position
stripped_filename = filename[0:file_extension_position]
print("Stripped Filename: {}".format(stripped_filename))
In Python 3.9, as part of PEP-616, you can now use the removeprefix
and removesuffix
functions:
>>> "Boat.txt".removeprefix("Boat")
>>> '.txt'
>>> "Boat.txt".removesuffix(".txt")
>>> 'Boat'
In addition to the other excellent answers, sometimes rpartiton
may also get you there (depends on the exact usecase).
>> "Boat.txt".rpartition('.txt')
('Boat', '.txt', '')
>> "Boat.txt".rpartition('.txt')[0]
'Boat'
>>> foo="Boat.txt"
>>> foo[:foo.rfind(".txt")]
'Boat'
Is it possible to use the python command rstrip
so that it does only remove one exact string and does not take all letters separately?
I was confused when this happened:
>>>"Boat.txt".rstrip(".txt")
>>>'Boa'
What I expected was:
>>>"Boat.txt".rstrip(".txt")
>>>'Boat'
Can I somehow use rstrip and respect the order, so that I get the second outcome?
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')
Starting with Python 3.9, use .removesuffix()
:
"Boat.txt".removesuffix(".txt")
On earlier versions of Python, you’ll have to either define it yourself:
def removesuffix(s, suf):
if suf and s.endswith(suf):
return s[:-len(suf)]
return s
(you need to check that suf
isn’t empty, otherwise removing an empty suffix e.g. removesuffix("boat", "")
will do return s[:0]
and return ""
instead of "boat"
)
or use regex:
import re
suffix = ".txt"
s = re.sub(re.escape(suffix) + '$', '', s)
>>> myfile = "file.txt"
>>> t = ""
>>> for i in myfile:
... if i != ".":
... t+=i
... else:
... break
...
>>> t
'file'
>>> # Or You can do this
>>> import collections
>>> d = collections.deque("file.txt")
>>> while True:
... try:
... if "." in t:
... break
... t+=d.popleft()
... except IndexError:
... break
... finally:
... filename = t[:-1]
...
>>> filename
'file'
>>>
This will work regardless of extension type.
# Find the rightmost period character
filename = "my file 1234.txt"
file_extension_position = filename.rindex(".")
# Substring the filename from first char up until the final period position
stripped_filename = filename[0:file_extension_position]
print("Stripped Filename: {}".format(stripped_filename))
In Python 3.9, as part of PEP-616, you can now use the removeprefix
and removesuffix
functions:
>>> "Boat.txt".removeprefix("Boat")
>>> '.txt'
>>> "Boat.txt".removesuffix(".txt")
>>> 'Boat'
In addition to the other excellent answers, sometimes rpartiton
may also get you there (depends on the exact usecase).
>> "Boat.txt".rpartition('.txt')
('Boat', '.txt', '')
>> "Boat.txt".rpartition('.txt')[0]
'Boat'
>>> foo="Boat.txt"
>>> foo[:foo.rfind(".txt")]
'Boat'