使用 pathlib 破坏性地将一个目录重命名为另一个现有目录

Use pathlib to destructively rename one directory to another existing directory

我的目录结构可能类似于

Data
    Current
        A
        B
        C
    Previous
        A
        X

在尽可能 simple/quick 的步骤中,我想将 Current 重命名为 Previous 包括内容并删除原来的内容,现在是:

 Data
    Previous
        A
        B
        C

我试过类似的方法:

from pathlib import Path
src = Path('Data/Current')
dest = Path('Data/Previous')
src.replace(dest)

文档让我希望这会起作用:

If target points to an existing file or directory, it will be unconditionally replaced.

但它确实似乎是有条件的。我收到 Directory not empty 异常。我想我可以先递归地删除 Previous 目录。这基本上是唯一的解决方案吗?或者有更好的方法来实现这个目标吗?

(我更喜欢pathlib,但如果osshutil是更好的锤子,我不反对他们)

(我 运行 在 Linux)

继续 sehafoc 的评论,在使用 shutil 删除 dest ("Data/Previous") 的整个文件树之后:

shutil.rmtree(dest)

src ("Data/Current") 重命名为 dest .

src.rename(dest)