You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
1.2 KiB

11 months ago
# -*- coding: utf-8 -*-
# Time : 2023/11/9 11:28
# Author : lirunsheng
# User : l'r's
# Software: PyCharm
# File : demo12.py
import os
import shutil
# 遍历指定文件夹并复制图片文件到新的文件夹
def copy_images(folder_path, target_folder_path):
# 创建目标文件夹(如果它不存在)
if not os.path.exists(target_folder_path):
os.makedirs(target_folder_path)
for root, dirs, files in os.walk(folder_path):
for file in files:
# 检查文件类型是否为图片类型(这里使用了简单的判断方式)
if file.endswith('.jpg') or file.endswith('.png'):
# 构造源文件的完整路径
source_file_path = os.path.join(root, file)
# 构造目标文件的完整路径
target_file_path = os.path.join(target_folder_path, file)
# 使用 shutil 模块的 copy2() 函数复制文件(保留源文件的元数据)
shutil.copy2(source_file_path, target_file_path)
# 调用函数来复制图片文件
copy_images('D:/weixin/WeChat Files/wxid_f2yvfdxbyuag22/FileStorage/File/2023-11/球/', 'K:/工作/traveler/background/')