def take(source_dir, destination_dir):
# 确保目标文件夹存在,如果不存在则创建
if not os.path.exists(destination_dir):
os.makedirs(destination_dir)
# 遍历源文件夹中的所有文件
for filename in os.listdir(source_dir):
# 检查文件扩展名是否为.json
if filename.endswith('.png'):
# 构建完整的文件路径
source_file = os.path.join(source_dir, filename)
destination_file = os.path.join(destination_dir, filename)
# 复制文件
shutil.copy(source_file, destination_file)
print(f"Copied {filename} to {destination_dir}")
Python实现从多个文件夹中拿走具有某特征的文件,放入新的文件夹
最新推荐文章于 2025-11-29 16:24:14 发布
本文介绍了如何使用Python的os和shutil模块编写一个名为`deftake`的函数,该函数遍历源文件夹中的`.png`文件,并将它们复制到目标目录,如果目标目录不存在则先创建。
2646

被折叠的 条评论
为什么被折叠?



