python代码:通过筛选实现将一个文件夹里的图片保存到另外两个文件夹下
时间: 2023-04-07 13:02:14 浏览: 124
可以回答这个问题。以下是示例代码:
```python
import os
import shutil
# 定义原始文件夹路径和目标文件夹路径
source_folder = "path/to/source/folder"
target_folder1 = "path/to/target/folder1"
target_folder2 = "path/to/target/folder2"
# 获取原始文件夹中所有图片的文件名
image_files = [f for f in os.listdir(source_folder) if f.endswith(".jpg") or f.endswith(".png")]
# 将图片复制到目标文件夹1
for image_file in image_files:
source_path = os.path.join(source_folder, image_file)
target_path = os.path.join(target_folder1, image_file)
shutil.copyfile(source_path, target_path)
# 将图片复制到目标文件夹2
for image_file in image_files:
source_path = os.path.join(source_folder, image_file)
target_path = os.path.join(target_folder2, image_file)
shutil.copyfile(source_path, target_path)
```
这段代码可以将一个文件夹中所有的图片文件(.jpg 或 .png)复制到另外两个文件夹中。
阅读全文