多线程搜索文件拷贝-Python脚本

单线程的文件拷贝太折磨人了,所以这里使用多线程的方式去拉满软件效率

import os
import shutil
import threading
import queue

class FileItem:
    """自定义文件对象类,用于存储文件路径和命名序号"""
    def __init__(self, path, index):
        self.path = path
        self.index = index

# 全局变量
file_queue = queue.Queue()
search_completed = False  # 标记搜索线程是否完成
lock = threading.Lock()

def search_bmp_images(source_folder):
    """搜索文件夹及其子文件夹中的所有 BMP 文件并将其放入队列中"""
    count = 1  # 用于命名文件的计数器
    for dirpath, _, filenames in os.walk(source_folder):
        for filename in filenames:
            if filename.lower().endswith('.bmp'):
                file_item = FileItem(os.path.join(dirpath, filename), count)
                file_queue.put(file_item)
                count += 1
    global search_completed
    search_completed = True  # 标记搜索完成

def copy_bmp_image(destination_folder):
    """从队列中取出 BMP 文件并复制到目标文件夹"""
    while True:
        if not file_queue.empty():
            file_item = file_queue.get()
            destination_file = os.path.join(destination_folder, f'{file_item.index}.bmp')
            shutil.copy(file_item.path, destination_file)
            print(f'已复制: {file_item.path} 到 {destination_file}')
            file_queue.task_done()
        elif search_completed:  # 如果搜索完成且队列为空,则退出
            break

def main(source_folder, destination_folder, num_workers):
    # 确保目标文件夹存在
    if not os.path.exists(destination_folder):
        os.makedirs(destination_folder)

    # 启动搜索线程
    search_thread = threading.Thread(target=search_bmp_images, args=(source_folder,))
    search_thread.start()

    # 启动多个复制线程
    threads = []
    for _ in range(num_workers):
        thread = threading.Thread(target=copy_bmp_image, args=(destination_folder,))
        thread.start()
        threads.append(thread)

    # 等待搜索线程完成
    search_thread.join()

    # 等待所有复制线程完成
    for thread in threads:
        thread.join()

source_folder = './search/'  # 替换为源文件夹的路径
destination_folder = './imgs/'  # 替换为目标文件夹的路径
num_workers = 4  # 设置工作线程数

main(source_folder, destination_folder, num_workers)

posted @ 2024-09-19 22:27  阿遇而已  阅读(4)  评论(0编辑  收藏  举报