首页 > 编程语言 > 详细

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

时间:2019-08-14 14:08:23      阅读:118      评论:0      收藏:0      [点我收藏+]

内容涉及:文件遍历,读取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)

 

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

原文:https://www.cnblogs.com/niulang/p/11351467.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!