用os的join遍历图片文件时出错了

用os的join遍历图片文件时出错了

import os
import tensorflow as tf
with tf.Session() as sess:
    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
    #遍历目录
    for root,dirs,file in os.walk('data/train/'):
            #载入图片
            image_data = tf.gfile.FastGFile(os.path.join(root,file), 'rb').read()
            predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})#图片格式是jpg格式
            predictions = np.squeeze(predictions)#把结果转为1维数据
 
            #打印图片路径及名称
            image_path = os.path.join(root,file)
            print(image_path)

报错信息:

File "D:\Anaconda3\envs\tensorflow-gpu\lib\genericpath.py", line 149, in _check_arg_types
    (funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'list'

求大神帮帮忙吧 !

阅读 2.3k
2 个回答

TypeError: join() argument must be str or bytes, not 'list',这个错误不是提示了吗,os.walk('data/train/'),你这个输出的file是list类型,os.path.join参数不能是list类型

菜鸟教程os.walk用法:http://www.runoob.com/python/...

for dirpath,subdirs,filenames in os.walk(path):
    for filename in filenames:
        fullname = os.path.join(dirpath, filename)
        ......
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题