GH-73991: Add pathlib.Path.copy_into() and move_into() (#123314)

These two methods accept an *existing* directory path, onto which we join
the source path's base name to form the final target path.

A possible alternative implementation is to check for directories in
`copy()` and `move()` and adjust the target path, which is done in several
`shutil` functions. This behaviour is helpful in a shell context, but
less so in a stored program that explicitly specifies destinations. For
example, a user that calls `Path('foo.py').copy('bar.py')` might not
imagine that `bar.py/foo.py` would be created, but under the alternative
implementation this will happen if `bar.py` is an existing directory.
This commit is contained in:
Barney Gale 2024-08-26 14:14:23 +01:00 committed by GitHub
parent dbc1752d41
commit c68a93c582
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 96 additions and 4 deletions

View file

@ -1575,6 +1575,18 @@ Copying, moving and deleting
.. versionadded:: 3.14
.. method:: Path.copy_into(target_dir, *, follow_symlinks=True, \
dirs_exist_ok=False, preserve_metadata=False, \
ignore=None, on_error=None)
Copy this file or directory tree into the given *target_dir*, which should
be an existing directory. Other arguments are handled identically to
:meth:`Path.copy`. Returns a new :class:`!Path` instance pointing to the
copy.
.. versionadded:: 3.14
.. method:: Path.rename(target)
Rename this file or directory to the given *target*, and return a new
@ -1633,6 +1645,15 @@ Copying, moving and deleting
.. versionadded:: 3.14
.. method:: Path.move_into(target_dir)
Move this file or directory tree into the given *target_dir*, which should
be an existing directory. Returns a new :class:`!Path` instance pointing to
the moved path.
.. versionadded:: 3.14
.. method:: Path.unlink(missing_ok=False)
Remove this file or symbolic link. If the path points to a directory,