python " />

扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

python复制文件并重命名的方法是什么

扬州沐宇科技
2023-10-18 14:10:05
python

在Python中,可以使用shutil模块的copy2函数来复制文件并重命名。

下面是一个示例代码:

import shutil
# 源文件路径
source_file = 'path/to/source_file.txt'
# 目标文件路径
target_file = 'path/to/target_file.txt'
# 复制文件并重命名
shutil.copy2(source_file, target_file)

上述代码会将源文件source_file.txt复制到目标文件target_file.txt,并保留源文件的元数据(如修改时间、权限等)。

注意,shutil.copy2函数可以用于复制文件夹和文件。如果想要复制整个文件夹,请使用shutil.copytree函数。

扫码添加客服微信