python复制图片到对应文件夹下

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 10 14:10:59 2018

@author: yxh
"""

import numpy as np
import os
import shutil
import sys


newpath = '/home/yxh/caffe/examples/Vegetable/20181210TEST-11/'



f = open("/home/yxh/caffe/examples/Vegetable/20181210TEST-11/test.txt")
files = f.readlines()
for pic in files:
    picname = pic.split(' ')[0]
    picpath = '/home/yxh/caffe/examples/Vegetable/VegetableImage11' + picname
    dir_name = picname.split('/')[1]
    pic_name = picname.split('/')[2]
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)
    pic_name, pic_extend = os.path.splitext(pic_name)
    new_path = os.path.join(newpath, dir_name) + '/' + pic_name + '.jpg'
    shutil.copy(picpath, new_path)

你可能感兴趣的:(python)