Python 根据csv内容复制指定文件到另一文件夹

import os
import csv
import shutil

def read_csv(csv_file_path):
    '''
    csv_file_path:  csv file absolute path
    todo:           get csv content
    '''
    with open(csv_file_path, 'r') as fp:
        reader = csv.DictReader(fp)
        reader.fieldnames = [name.strip() for name in reader.fieldnames]
        metal_artifact_list, motion_artifact_list, foreign_body_artifact_list, beam_hardening_artifact_list =[],[],[],[]
        for line in reader:
            if line['metal_artifact'] == '1':
                metal_artifact_list.append(str(int(line['slice'])-1).zfill(3) + '.dcm')
            if line['motion_artifact'] == '1':
                motion_artifact_list.append(str(int(line['slice'])-1).zfill(3) + '.dcm')
            if line['foreign_body_artifact'] == '1':
                foreign_body_artifact_list.append(str(int(line['slice'])-1).zfill(3) + '.dcm')
            if line['beam_hardening_artifact'] == '1':
                beam_hardening_artifact_list.append(str(int(line['slice'])-1).zfill(3) + '.dcm')
    return metal_artifact_list,motion_artifact_list,foreign_body_artifact_list,beam_hardening_artifact_list
            
def specific_dcm_copy(input_path,output_path,metal_list,motion_list,foreign_list,beam_list):
    '''
    four input: four artifact list 
    todo:       copy specific dcm according artifact list
    return:     no return
    '''
    if metal_list:
        metal_artifact_path = os.path.join(output_path,'metal_artifact')
        if not os.path.exists(metal_artifact_path):
            os.mkdir(metal_artifact_path)
        for dcm in metal_list:
            dcm_path = os.path.join(input_path, dcm)
            shutil.copy(dcm_path,metal_artifact_path)
            os.rename(os.path.join(metal_artifact_path,dcm), os.path.join(metal_artifact_path,os.path.basename(input_path)+'_'+dcm))
    if motion_list:
        motion_artifact_path = os.path.join(output_path,'motion_artifact')
        if not os.path.exists(motion_artifact_path):
            os.mkdir(motion_artifact_path)
        for dcm in motion_list:
            dcm_path = os.path.join(input_path, dcm)
            shutil.copy(dcm_path,motion_artifact_path)
            os.rename(os.path.join(motion_artifact_path,dcm), os.path.join(motion_artifact_path,os.path.basename(input_path)+'_'+dcm))
    if foreign_list:
        foreign_body_artifact_path = os.path.join(output_path,'foreign_body_artifact')
        if not os.path.exists(foreign_body_artifact_path):
            os.mkdir(foreign_body_artifact_path)
        for dcm in foreign_list:
            dcm_path = os.path.join(input_path, dcm)
            shutil.copy(dcm_path,foreign_body_artifact_path)
            os.rename(os.path.join(foreign_body_artifact_path,dcm), os.path.join(foreign_body_artifact_path,os.path.basename(input_path)+'_'+dcm))
    if beam_list:
        beam_hardening_artifact_path = os.path.join(output_path,'beam_hardening_artifact')
        if not os.path.exists(beam_hardening_artifact_path):
            os.mkdir(beam_hardening_artifact_path)
        for dcm in beam_list:
            dcm_path = os.path.join(input_path, dcm)
            shutil.copy(dcm_path,beam_hardening_artifact_path)
            os.rename(os.path.join(beam_hardening_artifact_path,dcm), os.path.join(beam_hardening_artifact_path,os.path.basename(input_path)+'_'+dcm))
def main():
    csv_root_path = r'F:\CTBrain\brain_artifact\ann'
    dcm_target_path = r'F:\CTBrain\brain_artifact\dcm'
    dcm_ori_path = r'F:\CTBrain\brain_artifact\image'
    csv_file_list = os.listdir(csv_root_path)
    for csv_file in csv_file_list:
        csv_file_path = os.path.join(csv_root_path,csv_file)
        print('csv file name {}'.format(csv_file_path))
        metal_list,motion_list,foreign_list,beam_list = read_csv(csv_file_path)
        if not os.path.exists(dcm_target_path):
            os.mkdir(dcm_target_path)
        dcm_ori_sub_path = os.path.join(dcm_ori_path, csv_file.split('.')[0])
        print(dcm_ori_sub_path)
        specific_dcm_copy(dcm_ori_sub_path,dcm_target_path,metal_list,motion_list,foreign_list,beam_list)
    
    
if __name__ == '__main__':
    main()

写的很蠢,先记录下来,以后有时间再优化

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值