Python | shutil.copystat() method
Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of copying and removal of files and directories.
shutil.copystat() method in Python is used to copy the permission bits, last access time, last modification time, and flags value from the given source path to given destination path.
The shutil.copystat() method does not affect the file content and owner and group information.
On Linux, this method also try to copy some extended attributes in addition to permission bits, last access time, last modification time, and flags value.
Syntax: shutil.copystat(source, destination, *, follow_symlinks = True)
source: A string representing the path of the source file.
destination: A string representing the path of the destination file.
follow_symlinks (optional) : The default value of this parameter is True. If it is False and source and destination both refers to a symbolic link then the shutil.copystat() method will operate on the symbolic links themselves rather than the files the symbolic links refer to.
Note: The ‘*’ in parameter list indicates that all following parameters (Here in our case ‘follow_symlinks’) are keyword-only parameters and they can be provided using their name, not as positional parameter.
Return Type: This method does not return any value.
Code: Use of shutil.copystat() method to copy metadata from source to destination path
Before using shutil.copystat() method: Source metadata: Permission bits: 664 Last access time: Mon Jun 10 00:37:16 2019 Last modification time: Thu Dec 27 00:15:23 2018 Destination metadata: Permission bits: 777 Last access time: Fri Apr 12 01:13:25 2019 Last modification time: Thu Apr 11 02:03:45 2019 After using shutil.copystat() method: Destination metadata: Permission bits: 664 Last access time: Mon Jun 10 00:37:16 2019 Last modification time: Thu Dec 27 00:15:23 2018 Permission bits, last access time and last modification time copied from source to destination successfully