import os
import re
import shutil
from concurrent.futures import ThreadPoolExecutor
# 源文件夹路径
source_folder = "folder1"
# 目标文件夹路径
destination_folder = "destination"
# 创建目标文件夹
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
def copy_files(camera_path, img_path, camera_files):
for camera_file in camera_files:
# 提取文件名中的数字
match = re.search(r'\D+(\d+)', camera_file)
if match:
series_number = match.group(1)
# 创建目标文件夹路径
folder_name = os.path.splitext(camera_file)[0] + series_number
folder_path = os.path.join(destination_folder, folder_name)
os.makedirs(folder_path, exist_ok=True)
# 复制 jpg 文件到目标文件夹
shutil.copy(os.path.join(camera_path, camera_file), folder_path)
# 复制关键字文件到目标文件夹
for keyword in ['A', 'B', 'C', 'D']:
keyword_files = [f for f in os.listdir(img_path) if f.startswith(keyword + series_number)]
for keyword_file in keyword_files:
shutil.copy(os.path.join(img_path, keyword_file), folder_path)
# 创建线程池
with ThreadPoolExecutor() as executor:
# 遍历源文件夹下的所有文件夹
for root, dirs, files in os.walk(source_folder):
for dir_name in dirs:
dir_path = os.path.join(root, dir_name)
# 检查是否存在 camera 和 readback 文件夹
if os.path.exists(os.path.join(dir_path, "camera")) and os.path.exists(os.path.join(dir_path, "readback")):
# 检查 camera 文件夹中是否存在 jpg 文件
camera_files = [f for f in os.listdir(os.path.join(dir_path, "camera")) if f.endswith('.jpg')]
if camera_files:
# 检查 readback/img 文件夹中是否存在以关键字开头的文件
img_folder = os.path.join(dir_path, "readback", "img")
# 提交任务给线程池处理
camera_path = os.path.join(dir_path, "camera")
img_path = os.path.join(dir_path, "readback", "img")
executor.submit(copy_files, camera_path, img_path, camera_files)
import os
import shutil
from concurrent.futures import ThreadPoolExecutor
# 源文件夹路径
source_folder = "folder1"
# 目标文件夹路径
destination_folder = "destination"
# 创建目标文件夹
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
def copy_files(camera_path, img_path, camera_files):
for camera_file in camera_files:
# 创建与 jpg 文件名相同的文件夹
folder_name = os.path.splitext(camera_file)[0]
folder_path = os.path.join(destination_folder, folder_name)
os.makedirs(folder_path, exist_ok=True)
# 复制 jpg 文件到目标文件夹
shutil.copy(os.path.join(camera_path, camera_file), folder_path)
# 复制关键字文件到目标文件夹
for keyword in ['A', 'B', 'C', 'D']:
keyword_files = [f for f in os.listdir(img_path) if keyword in f]
for keyword_file in keyword_files:
shutil.copy(os.path.join(img_path, keyword_file), folder_path)
# 创建线程池
with ThreadPoolExecutor() as executor:
# 遍历源文件夹下的所有文件夹
for root, dirs, files in os.walk(source_folder):
for dir_name in dirs:
dir_path = os.path.join(root, dir_name)
# 检查是否存在 camera 和 readback 文件夹
if os.path.exists(os.path.join(dir_path, "camera")) and os.path.exists(os.path.join(dir_path, "readback")):
# 检查 camera 文件夹中是否存在 jpg 文件
camera_files = [f for f in os.listdir(os.path.join(dir_path, "camera")) if f.endswith('.jpg')]
if camera_files:
# 检查 readback/img 文件夹中是否存在包含关键字的文件
img_folder = os.path.join(dir_path, "readback", "img")
# 提交任务给线程池处理
camera_path = os.path.join(dir_path, "camera")
img_path = os.path.join(dir_path, "readback", "img")
executor.submit(copy_files, camera_path, img_path, camera_files)