# 将search_id 添加到 p_samples.json 中对应sample 的 request_images 列表中
# 保存新的p_samples.json
import json
import os
import os.path as osp
from builtins import FileNotFoundError
import logging
from csv import DictReader
import shutil
def mergepicture(txt_path,outfile_path):
"""通过txt文件得到search_id,ret_id,ret_score """
with open(txt_path, 'r') as txt:
data_list = txt.readlines() # 读取全部行
for index,line in enumerate(data_list):
linedata =line.strip()
search_id =linedata.split(',')[0]
register_images = linedata.split(',')[1]
ret_score =linedata.split(',')[2]
'''
总文件夹&&一级文件夹名字 ***
job命名:ret_score_group_id_index/ group_id为***
register做主图,主图"00-"开头
'''
#生成job文件夹
job_name = f"{ret_score}_jingqu_30wan_v1002_{index}"
job_file = osp.join(outfile_path, job_name)
os.mkdir(job_file)
shutil.copy(search_id,job_file)
old_register_name = osp.basename(register_images)
# os.path.basename(),返回path最后的文件名。若path以/或\结尾,那么就会返回空值。
new_register_name = f"00-{old_register_name}"
""" python中的f是format函数的缩写,用于格式化输出。
format函数常见的用法是str.format(),其基本语法是通过{}和:来代替以前的%。"""
dst_register_path = osp.join(job_file, new_register_name)
# os.path.join()函数用于路径拼接文件路径
shutil.copy(register_images, dst_register_path)
# shutil.copyfile(src, dst):复制文件内容(不包含元数据)从src到dst。
if __name__ == '__main__':
txt_path = '/da_score.txt'
outfile_path = '/data/94/'
mergepicture(txt_path,outfile_path)