GH-73991: Add pathlib.Path.move() (#122073)

Add a `Path.move()` method that moves a file or directory tree, and returns a new `Path` instance pointing to the target.

This method is similar to `shutil.move()`, except that it doesn't accept a *copy_function* argument, and it doesn't check whether the destination is an existing directory.
This commit is contained in:
Barney Gale 2024-08-25 16:51:51 +01:00 committed by GitHub
parent aa905925e1
commit 625d0705b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 225 additions and 4 deletions

View file

@ -1536,8 +1536,8 @@ Creating files and directories
available. In previous versions, :exc:`NotImplementedError` was raised.
Copying, renaming and deleting
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Copying, moving and deleting
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. method:: Path.copy(target, *, follow_symlinks=True, dirs_exist_ok=False, \
preserve_metadata=False, ignore=None, on_error=None)
@ -1616,6 +1616,23 @@ Copying, renaming and deleting
Added return value, return the new :class:`!Path` instance.
.. method:: Path.move(target)
Move this file or directory tree to the given *target*, and return a new
:class:`!Path` instance pointing to *target*.
If the *target* doesn't exist it will be created. If both this path and the
*target* are existing files, then the target is overwritten. If both paths
point to the same file or directory, or the *target* is a non-empty
directory, then :exc:`OSError` is raised.
If both paths are on the same filesystem, the move is performed with
:func:`os.replace`. Otherwise, this path is copied (preserving metadata and
symlinks) and then deleted.
.. versionadded:: 3.14
.. method:: Path.unlink(missing_ok=False)
Remove this file or symbolic link. If the path points to a directory,