这是一个使用Python编写的将图片从一个文件夹复制到另一个文件夹的示例代码。请确保目标文件夹为空,否则代码将无法正确运行。
import os
import shutil
def copy_images(source_folder, destination_folder):
# 获取源文件夹中的所有文件名
files = os.listdir(source_folder)
# 遍历源文件夹中的文件
for file_name in files:
# 构建源文件和目标文件的路径
source_file = os.path.join(source_folder, file_name)
destination_file = os.path.join(destination_folder, file_name)
# 如果是文件且是图片类型
if os.path.isfile(source_file) and file_name.endswith(('.jpg', '.png', '.jpeg')):
# 复制文件到目标文件夹
shutil.copy2(source_file, destination_file)
# 设置源文件夹和目标文件夹的路径
source_folder = 'path/to/source/folder'
destination_folder = 'path/to/destination/folder'
# 调用函数进行复制
copy_images(source_folder, destination_folder)
请将path/to/source/folder
替换为实际的源文件夹路径,将path/to/destination/folder
替换为实际的目标文件夹路径。