【问题标题】:Is Path.replace equivalent to os.replace or shutil.move?Path.replace 是否等同于 os.replace 或 shutil.move?
【发布时间】:2019-08-14 10:25:28
【问题描述】:

pathlib.Path.replace 方法的文档说明:

将此文件或目录重命名为给定的目标。如果目标指向 已存在的文件或目录,将被无条件替换。

这缺少一点细节。为了比较,这里是os.replace的文档:

将文件或目录src 重命名为dst。如果dst 是一个目录, OSError 将被提出。如果dst 存在并且是一个文件,它将是 如果用户有权限,则静默替换。操作可能会失败 如果 srcdst 在不同的文件系统上。如果成功,则 重命名将是一个原子操作(这是 POSIX 要求)。

重要的部分是 “如果 srcdst 在不同的文件系统上,操作可能会失败”。不像os.replaceshutil.move没有这个问题:

如果目标在当前文件系统上,那么os.rename() 是 用过的。否则,src 使用 copy_function 复制到 dst,然后 删除。

那么,Path.replace 使用了哪些函数?是否存在Path.replace 失败的风险,因为目标位于不同的文件系统上?

【问题讨论】:

    标签: python file-rename pathlib


    【解决方案1】:

    Path(x).replace(y) 只是调用os.replace(x, y)。你可以在the source code看到这个:

    class _NormalAccessor(_Accessor):
        # [...]
        replace = os.replace
        # [...]
    
    _normal_accessor = _NormalAccessor()
    
    # [...]
    
    class Path(PurePath):
        # [...]
        def _init(self,
                  # Private non-constructor arguments
                  template=None,
                  ):
            self._closed = False
            if template is not None:
                self._accessor = template._accessor
            else:
                self._accessor = _normal_accessor
    
        # [...]
    
        def replace(self, target):
            """
            Rename this path to the given path, clobbering the existing
            destination if it exists.
            """
            if self._closed:
                self._raise_closed()
            self._accessor.replace(self, target)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2017-11-29
      • 2010-11-30
      • 2017-01-20
      • 2016-02-12
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多