Python| shutil.copymode() 方法
Python中的Shutil 模块提供了许多对文件和文件集合进行高级操作的功能。它属于 Python 的标准实用程序模块。该模块有助于自动化复制和删除文件和目录的过程。
Python中的shutil.copymode()方法用于将权限位从给定的源路径复制到给定的目标路径。
shutil.copymode()方法不会影响文件内容或所有者和组信息。
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.
代码:使用 shutil.copymode() 方法将权限位从源路径复制到目标路径
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:])
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
参考: https://docs。 Python.org/3/library/shutil.html