Correct way to remove all file extensions from a path with pathlib?

Hi aoeu,

You have two lines of code. If you think that’s “verbose”, you should
see the actual implementation of pathlib (1500+ lines) wink

If you are worried about having to repeat those two lines in multiple
places, you can write a helper function.

By the way, if you are working with arbitrary files of unknown file
types, you should be aware that many files use dots in the actual
filename part, often but not always in place of spaces. It might be
worth checking the suffix against a set of the suffixes you are
expecting:

while filename.suffix in {'.tar', '.gz', '.zip'}:
    filename = filename.with_suffix('')
1 Like