Add `make_parents` arg to `pathlib.Path.rename` and `.replace`

I think it would be nice to have a make_parents arg for pathlib.Path.rename and pathlib.Path.replace.

def rename(self, target: pathlib.Path, make_parents: bool = False)
def replace(self, target: pathlib.Path, make_parents: bool = False)

Implementation should be pretty trivial, with a proof-of-concept like so:

if make_parents:
    target.parent.mkdir(parents=True, exist_ok=True)

os.rename(self, target)
return self.__class__(target)

and similar for replace.

In order to keep the API similar to mkdir, we could use parents instead of make_parents.

This would diverge the API from os.replace and os.rename, though I’m not sure how critical that API unity is now that pathlib is mature.

I don’t like overloading the role of rename and replace, and the loss of explicitness, just to save one line of code

1 Like

We are purposefully trying not to grow pathlib into having every potential helper feature. We want the API to stay small and tight and let external libraries provide extra features as necessary. So using os to help constrain API growth is actually helpful.