Open In App

Python | shutil.copystat() method

Improve
Improve
Like Article
Like
Save
Share
Report

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
 



Last Updated : 07 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads