TypeError:预期的str、bytes或os.PathLike对象,读取文件夹中的文件时不会出现列表错误



我想让all_image_files检索"/input/hubmap-organ-segmentation/train_images/"文件夹中。不确定为什么我的代码会引发"TypeError:预期str, bytes或os。类路径对象,而不是列表"错误。

class config:
BASE_PATH = "./input/hubmap-organ-segmentation/"
TRAIN_PATH = os.path.join(BASE_PATH, "train")
# wandb config
WANDB_CONFIG = {
'competition': 'HuBMAP', 
'_wandb_kernel': 'neuracort'
}
# Initialize W&B
run = wandb.init(
project='hubmap-organ-segmentation', 
config= WANDB_CONFIG
)
df = pd.read_csv(
os.path.join(config.BASE_PATH, "train.csv")
)

image_files = glob.glob(config.BASE_PATH + "/train_images/*")
all_image_files = glob(os.path.join(image_files, "*.tiff"), recursive=True)
train_img_map = {int(x[:-5].rsplit("/", 1)[-1]):x for x in all_image_files}

回溯:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [63], in <cell line: 1>()
----> 1 all_image_files = glob(os.path.join(image_files, "*.tiff"), recursive=True)
2 all_image_labels = glob(os.path.join(image_labels, "*.json"), recursive=True)
4 train_img_map = {int(x[:-5].rsplit("/", 1)[-1]):x for x in all_image_files}
File ~/anaconda3/lib/python3.9/posixpath.py:76, in join(a, *p)
71 def join(a, *p):
72     """Join two or more pathname components, inserting '/' as needed.
73     If any component is an absolute path, all previous path components
74     will be discarded.  An empty last part will result in a path that
75     ends with a separator."""
---> 76     a = os.fspath(a)
77     sep = _get_sep(a)
78     path = a
TypeError: expected str, bytes or os.PathLike object, not list

问题是您通过调用glob获得image_files,因此image_files是路径列表。这会在下一行代码中导致问题,因为您尝试执行os.path.join(image_files, "*.tiff"),而os.path.join的参数应该是字符串,而不是列表。

如果您想获得./input/hubmap-organ-segmentation/train_images及其所有后代中的所有.tiff文件,那么您可以删除image_files并在一次调用中获得文件列表:

import glob
all_image_files = glob.glob(os.path.join(BASE_PATH, "train_images", "**", "*.tiff"), recursive=True)

可以吗?

相关内容

  • 没有找到相关文章

最新更新