多线程移动文件

import glob
import os
import shutil
import time
import threading
from tqdm import tqdm # pip install tqdm

def move_file(source, destination):
    if os.path.exists(os.path.join(destination, os.path.basename(source))):
        return
    try:
        shutil.move(source, destination)
    except Exception as e:
        print(f"Error moving {source}: {str(e)}")

if __name__ == '__main__':
    input_path = "./test"
    export_path = "./test_output"
    name_len = 5
    start = time.time()
    pathi = input_path+"/**.jpg"
    files = glob.glob(pathi)

    thread_num = 10
    chunk_size = len(files) // thread_num
    threades = []
    with tqdm(total=len(files)) as pbar: # 进度条
        def update_progress():
            pbar.update()
        for i in range(thread_num):
            start_index = i * chunk_size
            end_index = start_index + chunk_size

            if i == thread_num - 1:
                # Handle the last chunk in case of uneven division
                end_index = len(files)

            files_to_move = files[start_index:end_index]

            for file_name in files_to_move:
                out_path0 = os.path.basename(file_name).split("_")
                out_path1 = ""
                for i in range(name_len):
                    if i == name_len - 1:
                        out_path1 = out_path1+out_path0[i]
                    else:
                        out_path1 = out_path1+out_path0[i]+"_"

                out_path = os.path.join(export_path, out_path1)
                if not os.path.exists(out_path):
                    os.makedirs(out_path)
                result = os.path.join(out_path, "output_result")
                if not os.path.exists(result):
                    os.makedirs(result)
                thread  =  threading.Thread(target=move_file, args=(file_name, result))
                thread.start()
                threades.append(thread)
                update_progress()
    for thread in threades:
        thread.join()
    end = time.time()
    print(end-start)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值