(src, dst, *, follow_symlinks=True)
Copy file metadata

    Copy the permission bits, last access time, last modification time, and
    flags from `src` to `dst`. On Linux, copystat() also copies the "extended
    attributes" where possible. The file contents, owner, and group are
    unaffected. `src` and `dst` are path-like objects or path names given as
    strings.

    If the optional flag `follow_symlinks` is not set, symlinks aren't
    followed if and only if both `src` and `dst` are symlinks.
Copy file metadata
Copy file metadata
def copystat(src, dst, *, follow_symlinks=True):
    """Copy file metadata

    Copy the permission bits, last access time, last modification time, and
    flags from `src` to `dst`. On Linux, copystat() also copies the "extended
    attributes" where possible. The file contents, owner, and group are
    unaffected. `src` and `dst` are path-like objects or path names given as
    strings.

    If the optional flag `follow_symlinks` is not set, symlinks aren't
    followed if and only if both `src` and `dst` are symlinks.
    """
    sys.audit("shutil.copystat", src, dst)

    def _nop(*args, ns=None, follow_symlinks=None):
        pass

    # follow symlinks (aka don't not follow symlinks)
    follow = follow_symlinks or not (_islink(src) and os.path.islink(dst))
    if follow:
        # use the real function if it exists
        def lookup(name):
            return getattr(os, name, _nop)
    else:
        # use the real function only if it exists
        # *and* it supports follow_symlinks
        def lookup(name):
            fn = getattr(os, name, _nop)
            if fn in os.supports_follow_symlinks:
                return fn
            return _nop

    if isinstance(src, os.DirEntry):
        st = src.stat(follow_symlinks=follow)
    else:
        st = lookup("stat")(src, follow_symlinks=follow)
    mode = stat.S_IMODE(st.st_mode)
    lookup("utime")(dst, ns=(st.st_atime_ns, st.st_mtime_ns),
        follow_symlinks=follow)
    # We must copy extended attributes before the file is (potentially)
    # chmod()'ed read-only, otherwise setxattr() will error with -EACCES.
    _copyxattr(src, dst, follow_symlinks=follow)
    try:
        lookup("chmod")(dst, mode, follow_symlinks=follow)
    except NotImplementedError:
        # if we got a NotImplementedError, it's because
        #   * follow_symlinks=False,
        #   * lchown() is unavailable, and
        #   * either
        #       * fchownat() is unavailable or
        #       * fchownat() doesn't implement AT_SYMLINK_NOFOLLOW.
        #         (it returned ENOSUP.)
        # therefore we're out of options--we simply cannot chown the
        # symlink.  give up, suppress the error.
        # (which is what shutil always did in this circumstance.)
        pass
    if hasattr(st, 'st_flags'):
        try:
            lookup("chflags")(dst, st.st_flags, follow_symlinks=follow)
        except OSError as why:
            for err in 'EOPNOTSUPP', 'ENOTSUP':
                if hasattr(errno, err) and why.errno == getattr(errno, err):
                    break
            else:
                raise
def copystat ( src , dst , * , follow_symlinks = True ) : sys . audit ( "shutil.copystat" , src , dst ) def _nop ( * args , ns = None , follow_symlinks = None ) : pass # follow symlinks (aka don't not follow symlinks) follow = follow_symlinks or not ( _islink ( src ) and os . path . islink ( dst ) ) if follow : # use the real function if it exists def lookup ( name ) : return getattr ( os , name , _nop ) else : # use the real function only if it exists # *and* it supports follow_symlinks def lookup ( name ) : fn = getattr ( os , name , _nop ) if fn in os . supports_follow_symlinks : return fn return _nop if isinstance ( src , os . DirEntry ) : st = src . stat ( follow_symlinks = follow ) else : st = lookup ( "stat" ) ( src , follow_symlinks = follow ) mode = stat . S_IMODE ( st . st_mode ) lookup ( "utime" ) ( dst , ns = ( st . st_atime_ns , st . st_mtime_ns ) , follow_symlinks = follow ) # We must copy extended attributes before the file is (potentially) # chmod()'ed read-only, otherwise setxattr() will error with -EACCES. _copyxattr ( src , dst , follow_symlinks = follow ) try : lookup ( "chmod" ) ( dst , mode , follow_symlinks = follow ) except NotImplementedError : # if we got a NotImplementedError, it's because #   * follow_symlinks=False, #   * lchown() is unavailable, and #   * either #       * fchownat() is unavailable or #       * fchownat() doesn't implement AT_SYMLINK_NOFOLLOW. #         (it returned ENOSUP.) # therefore we're out of options--we simply cannot chown the # symlink.  give up, suppress the error. # (which is what shutil always did in this circumstance.) pass if hasattr ( st , 'st_flags' ) : try : lookup ( "chflags" ) ( dst , st . st_flags , follow_symlinks = follow ) except OSError as why : for err in 'EOPNOTSUPP' , 'ENOTSUP' : if hasattr ( errno , err ) and why . errno == getattr ( errno , err ) : break else : raise