Python | shutil.copystat() method
Last Updated :
07 Jul, 2021
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)
Parameter:
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
Python3
# Python program to explain shutil.copystat() method
# importing os module
import os
# importing shutil module
import shutil
# importing time module
import time
# Source file path
src = "/home/ihritik/Desktop/sam3.pl"
# Destination file path
dest = "/home/ihritik/Desktop/encry.py"
# Print the permission bits
# last access time, last modification time
# and flags value of source and destination files
print("Before using shutil.copystat() method:")
print("Source metadata:")
print("Permission bits:", oct(os.stat(src).st_mode)[-3:])
print("Last access time:", time.ctime(os.stat(src).st_atime))
print("Last modification time:", time.ctime(os.stat(src).st_mtime))
# print("User defined Flags:", os.stat(src).st_flags)
# Note: st_flags attribute is platform dependent
# and is subject to availability
print("\nDestination metadata:")
print("Permission bits:", oct(os.stat(dest).st_mode)[-3:])
print("Last access time:", time.ctime(os.stat(dest).st_atime))
print("Last modification time:", time.ctime(os.stat(dest).st_mtime))
# print("User defined Flags:", os.stat(dest).st_flags)
# Copy the permission bits
# last access time, last modification time
# and flags value from source to destination
shutil.copystat(src, dest)
# Print the permission bits
# last access time, last modification time
# and flags value of destination
print("\nAfter using shutil.copystat() method:")
print("Destination metadata:")
print("Permission bits:", oct(os.stat(dest).st_mode)[-3:])
print("Last access time:", time.ctime(os.stat(dest).st_atime))
print("Last modification time:", time.ctime(os.stat(dest).st_mtime))
# print("User defined Flags:", os.stat(dest).st_flags)
print("Permission bits, last access time and last modification time\n\
copied from source to destination successfully")
Output: 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
Reference: https://docs.python.org/3/library/shutil.html
Similar Reads
Python | shutil.copy() method Python3 # Python program to explain shutil.copy() method # importing shutil module import shutil # Source path source = "/home/User/Documents/file.txt" # Destination path destination = "/home/User/Documents/file.txt" # Copy the content of # source to destination try: shutil.copy(
5 min read
Python | shutil.copy2() 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.copy2() method in Python is used to copy the co
4 min read
shutil.copytree() method - Python shutil.copytree() method in Python is used to recursively copy an entire directory tree from a source to a destination. It copies all the contents, including files and subdirectories, preserving the directory structure.Syntaxshutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
3 min read
shutil.copytree() method - Python shutil.copytree() method in Python is used to recursively copy an entire directory tree from a source to a destination. It copies all the contents, including files and subdirectories, preserving the directory structure.Syntaxshutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
3 min read
Python | shutil.copyfile() method Shutil module in Python helps automate the process of copying and removing files and directories. It comes under Pythonâs standard utility modules. Shutil(short for shell utility) module also provides many functions of high-level operations on files and collections of files. What is Shutil.copyfile
4 min read
Python | shutil.copyfile() method Shutil module in Python helps automate the process of copying and removing files and directories. It comes under Pythonâs standard utility modules. Shutil(short for shell utility) module also provides many functions of high-level operations on files and collections of files. What is Shutil.copyfile
4 min read