Python| shutil.copystat() 方法
Python中的Shutil 模块提供了许多对文件和文件集合进行高级操作的功能。它属于 Python 的标准实用程序模块。该模块有助于自动复制和删除文件和目录的过程。
Python中的shutil.copystat()方法用于将权限位、上次访问时间、上次修改时间和标志值从给定的源路径复制到给定的目标路径。
shutil.copystat()方法不会影响文件内容以及所有者和组信息。
在 Linux 上,除了权限位、上次访问时间、上次修改时间和标志值之外,此方法还尝试复制一些扩展属性。
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.
代码:使用 shutil.copystat() 方法将元数据从源路径复制到目标路径
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")
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
参考: https://docs。 Python.org/3/library/shutil.html