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)
多线程移动文件
最新推荐文章于 2024-05-13 20:22:08 发布