0
点赞
收藏
分享

微信扫一扫

python 按照有序文件名顺序读取


用 os.listdir(path) 会打乱原有的文件顺序 , 需要进行一下排序 。


文件组织为 : 

python 按照有序文件名顺序读取_tensorflow

代码 : 

 

import tensorflow as tf
import numpy as np
import os
image_name = 'img_4.jpg'
trainImage_path = r'./garbage_classify/train_data'
image_files = os.listdir(trainImage_path)
image_files.sort(key=lambda x : int(x.split('.')[0].split('_')[1] ) )
numOFimages = 0
for image_file in image_files :
if image_file.endswith('.jpg') :
numOFimages+= 1
else:
# print("文件名 : " , image_file)
with open(trainImage_path+'/'+image_file ,'r') as fr :
try:
info = fr.readlines()
key = info[0].strip('').split(',')[0]
value = info[0].strip('').split(',')[1]
classify[key] = value

except FileNotFoundError:
print("文件未找到")

 

举报

相关推荐

0 条评论