shutil.copy2复制文件到指定文件夹
复制文件到指定文件夹
import shutil
import os
def copy_file(source_path, destination_folder):
# 确保目标文件夹存在
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
# 构造目标文件的完整路径
destination_path = os.path.join(destination_folder, os.path.basename(source_path))
# 使用 shutil 进行文件复制
shutil.copy2(source_path, destination_path)
# 示例用法
source_file = 'path/to/your/source/file.txt'
destination_folder = 'path/to/your/destination/folder'
copy_file(source_file, destination_folder)