📜  Python| shutil.copymode() 方法

📅  最后修改于: 2022-05-13 01:55:27.616000             🧑  作者: Mango

Python| shutil.copymode() 方法

Python中的Shutil 模块提供了许多对文件和文件集合进行高级操作的功能。它属于 Python 的标准实用程序模块。该模块有助于自动化复制和删除文件和目录的过程。
Python中的shutil.copymode()方法用于将权限位从给定的源路径复制到给定的目标路径。
shutil.copymode()方法不会影响文件内容或所有者和组信息。

代码:使用 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