Python | shutil.copystat() method - GeeksforGeeks

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 moduleimport os # importing shutil moduleimport shutil # importing time moduleimport time  # Source file pathsrc = "/home/ihritik/Desktop/sam3.pl"  # Destination file pathdest = "/home/ihritik/Desktop/encry.py"  # Print the permission bits# last access time, last modification time# and flags value of source and destination filesprint("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 destinationshutil.copystat(src, dest)  # Print the permission bits# last access time, last modification time# and flags value of destinationprint("\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")
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 
surindertarika1234
surinderdawra388
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Enumerate() in Python
Read a file line by line in Python
Defaultdict in Python
Different ways to create Pandas Dataframe
sum() function in Python
Iterate over a list in Python
Deque in Python
How to Install PIP on Windows ?
Python String | replace()