Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | shutil.copymode() method

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




# Python program to explain shutil.copymode() method
   
# importing os module
import os
 
# importing shutil module
import shutil
 
 
# Source file path
src = "/home/ihritik/Desktop/sam3.pl"
 
 
# Destination file path
dest = "/home/ihritik/Desktop/encry.py"
 
# Print the permission bits
# of source and destination path
 
# As we know, st_mode attribute
# of ‘stat_result’ object returned
# by os.stat() method is an integer
# which represents file type and
# file mode bits (permissions)
# So, here integere is converted into octal form
# to get typical octal permissions.
# Last 3 digit represents the permission bits
# and rest digits represents the file type
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:])
     
 
# Copy the permission bits
# from source to destination
shutil.copymode(src, dest)
 
 
# Print the permission bits
# of destination path
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
 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments