python文件处理-根据csv文件内容,将对应图像拷贝到指定文件夹

内容涉及:文件遍历,读取csv指定列,拷贝文件,清理和创建文件

# -*- coding: utf-8 -*-
import csv
import os
import sys
import numpy as np
import copy
import shutil
import pandas as pd
from collections import Counter
from shutil import copyfile

'''
原数据目录如下:
./2019-6-14/
├── 1816740
│   ├── IMG005x020.csv
│   ├── IMG027x033.JPG
│   ├── IMG029x023.csv
│   └── IMG029x023.JPG
        ... ...
├── 1816765
│   └── ... ...
├── 1816875
│   └── ... ...
├── 1816896
│   └── ... ...
├── 1816900
│   └── ... ...
└── 1816969
    └── ... ...
    ...
分类脚本与/2019-6-14/在同级目录
'''

data = '2019-6-23'
path = os.getcwd()
path_1 = path + '/' + data
data_N_root = path + '/' + 'data_N' + '_' + data
data_P_root = path + '/' + 'data_P' + '_' + data
if os.path.exists(data_N_root):
    shutil.rmtree(data_N_root+'/')
os.mkdir(data_N_root)
if os.path.exists(data_P_root):
    shutil.rmtree(data_P_root+'/')
os.mkdir(data_P_root)
print("清理文件夹")
print(data_N_root)
print(data_P_root)
list_name = os.listdir(path_1)
lossfile = []
count1 = 0
count2 = 0
for n in list_name:
    path_2 = path_1 + '/' + n
    list_name_1 = os.listdir(path_2)
    for m in list_name_1:
        temp = copy.copy(m[-3:])
        temp_1 = copy.copy(m[:-3])
        csv_root = path_1 + '/' + n + '/' + m
            
        if temp == 'csv':
            if not os.path.exists(csv_root[:-3] + 'JPG'):
                lossfile.append(csv_root)
                continue
            with open(csv_root,'rwb') as csvfile:
                reader = csv.reader(csvfile)
                column1 = [row[0]for row in reader]
                column1 = column1[1:]
                #print("len",len(column1))
                column2 = str(np.ones((len(column1),1)))
                print(column1)
                print(column2)
                print(csv_root)
                writer = csv.writer(csvfile)
                for val in column2:
                    writer.writerow('type',val)
                print("cell type :", column1)
                sign = 0
                for k in column1:
                    if k == '1' or k == '5':
                        continue
                    else:
                        sign = 1
                print("sign :", sign)
                if sign == 1:
                    print("该FOV为阳性")
                    copyfile(csv_root[:-3] + 'csv',data_P_root + '/' + temp_1 + 'csv')
                    copyfile(csv_root[:-3] + 'JPG',data_P_root + '/' + temp_1 + 'JPG')
                    count1 = count1 + 1
                else:
                    print("该FOV为阴性")
                    copyfile(csv_root[:-3] + 'csv',data_N_root + '/' + temp_1 + 'csv')
                    copyfile(csv_root[:-3] + 'JPG',data_N_root + '/' + temp_1 + 'JPG')
                    count2 = count2 + 1

print("===============================")
if len(lossfile) == 0:
    print("data classify OK")
else:
    print("loss file :")
    for n in lossfile:
        print(n)
print("阳性FOV数量:",count1)
print("阴性FOV数量:",count2)

 

posted @ 2019-08-14 13:49  牛郎  阅读(1347)  评论(0编辑  收藏  举报