python多线程

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)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值