.strip() removes each character from both ends of the string, not a complete substring, potentially leading to (or preventing) disaster!
>>> "texted_my_ex.txt".strip(".txt")
'exted_my_e'
The string methods .rstrip() and .lstrip() also exist to remove only from the right or left sides respectively, but will still consume characters until they run out of matches rather than matching all at once
Whenever you have paths or filenames (filenames are just short paths!), pathlib.Path is almost-certainly the most practical answer, as it provides additional methods for reaching different properties of the path, such as the extension (.suffix), making it relative to another path (.relative_to()), checking if it exists, renaming it, etc.
Specifically pathlib.Path(filename).stem should work for your and any inputs1
from pathlib import Path
filenames = ["report.txt", "downloads.txt", "success.txt", "folders.txt"]
for path in map(Path, filenames):
print(f"{path.stem: <10} + {path.suffix} from {path}")
report + .txt from report.txt
downloads + .txt from downloads.txt
success + .txt from success.txt
folders + .txt from folders.txt
more features (note / is used to append, not +, and it will accept strings when adding more path to a Path instance)
>>> p = Path("report.txt")
>>> p.exists()
False
>>> Path("/root") / "some_very/long_path/middle_part" / p
PosixPath('/root/some_very/long_path/middle_part/report.txt')
Path will also handle multiple extensions in a nice way
>>> p = Path("somewhere/foo.tar.gz")
>>> p
PosixPath('somewhere/foo.tar.gz')
>>> p.suffix
'.gz'
>>> p.suffixes
['.tar', '.gz']
- take extra caution if you think you can have files with multiple extensions or dots in their path and need a very generic method .. unfortunately you may not be able to get away from doing some string parsing, though often asserting true things about files helps ensure correctness (such as enforcing a regex over input file names when users attempt to create or supply something silly)
>>> path_str = "base/title.d/myfile.first.second.txt2"
>>> path_str[:-4] # not very robust
'base/title.d/myfile.first.second.'
>>> path_str.split(".")[0] # doesn't work for . in pathname
'base/title'
>>> p = Path(path_str)
>>> p.name # can get complete name
'myfile.first.second.txt2'
>>> p.suffix # only the final suffix
'.txt2'
>>> p.name.split('.')[0] # potentially the most-correct
'myfile'