在Python中,可以使用shutil
模块的copy2
函数来复制文件,并保留原始的创建日期。下面是一个包含代码示例的解决方法:
import shutil
import os
def copy_file_with_original_create_date(source_file, destination_file):
shutil.copy2(source_file, destination_file)
# 示例用法
source_file = 'path/to/source/file.txt'
destination_file = 'path/to/destination/file.txt'
copy_file_with_original_create_date(source_file, destination_file)
上述代码中,copy_file_with_original_create_date
函数接受两个参数:source_file
和destination_file
,分别表示源文件和目标文件的路径。函数内部使用shutil.copy2
函数来复制文件,并保留原始的创建日期。
在示例用法中,你需要将source_file
和destination_file
替换为实际的文件路径。调用copy_file_with_original_create_date
函数后,源文件将被复制到目标文件,并且目标文件的创建日期将与源文件的创建日期保持一致。