python遍历文件夹生成标签数据txt文件

# coding=UTF-8

import os
import sys

path="/home/cdli/Original_10.18"
filenames=os.listdir(path) #读取path内所有文件名返回列表
i=0
with open("train_color.txt","w") as f:
        for filename in filenames:
                sub_path=path+"/"+filename+"/"
                pic=os.listdir(sub_path)
                print(filename+','+str(i))
                for p in pic:
                        f.write('XXX/'+filename+'/'+p+' '+str(i)+'\n')
                i=i+1

最终生成的txt就是:
XXX 0
XXX 0
XXX 1
XXX 1
XXX 2
XXX 2

你可能感兴趣的:(python,caffe)