[Python-ideas] Another pathlib suggestion

Antony Lee antony.lee at berkeley.edu
Wed May 21 22:38:01 CEST 2014


Handling of Paths with multiple extensions is currently not so easy with
pathlib.  Specifically, I don't think there is an easy way to go from
"foo.tar.gz" to "foo.ext", because Path.with_suffix only replaces the last
suffix.

I would therefore like to suggest either

1/ add Path.replace_suffix, such that
    Path("foo.tar.gz").replace_suffix(".tar.gz", ".ext") == Path("foo.ext")
(this would also provide extension-checking capabilities, raising
ValueError if the first argument is not a valid suffix of the initial
path); or

2/ add a second argument to Path.with_suffix, "n_to_strip" (although
perhaps with a better name), defaulting to 1, such that
    Path("foo.tar.gz").with_suffix(".ext", 0) == Path("foo.tar.gz.ext")
    Path("foo.tar.gz").with_suffix(".ext", 1) == Path("foo.tar.ext")
    Path("foo.tar.gz").with_suffix(".ext", 2) == Path("foo.ext") # set
n_to_strip to len(path.suffixes) for stripping all of them.
    Path("foo.tar.gz").with_suffix(".ext", 3) raises a ValueError.

Best,
Antony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140521/a4b5a2a3/attachment.html>


More information about the Python-ideas mailing list