Python | shutil.copymode() 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 the process of copying and removal of files and directories.
shutil.copymode() method in Python is used to copy the permission bits from the given source path to given destination path.
The shutil.copymode() method does not affect the file content or owner and group information.
Syntax: shutil.copymode(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.copymode() method will try to modify the mode of destination (which is a symbolic link ) itself rather than the file it points 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.copymode() method to copy permission bits from source path to destination path
Python3
import os
import shutil
src = "/home/ihritik/Desktop/sam3.pl"
dest = "/home/ihritik/Desktop/encry.py"
print ( "Before using shutil.copymode() method:" )
print ( "Permission bits of source:" , oct (os.stat(src).st_mode)[ - 3 :])
print ( "Permission bits of destination:" , oct (os.stat(dest).st_mode)[ - 3 :])
shutil.copymode(src, dest)
print ( "\nAfter using shutil.copymode() method:" )
print ( "Permission bits of destination:" , oct (os.stat(dest).st_mode)[ - 3 :])
|
Output:
Before using shutil.copymode() method:
Permission bits of source: 664
Permission bits of destination: 777
After using shutil.copymode() method:
Permission bits of destination: 664
Reference: https://docs.python.org/3/library/shutil.html
Last Updated :
04 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...