melolilili
melolilili

Reputation: 227

TypeError: expected str, bytes or os.PathLike object, not list error when reading files in a folder

I want all_image_files to retrieve all the training images in "./input/hubmap-organ-segmentation/train_images/" folder. Not sure why my code is raising "TypeError: expected str, bytes or os.PathLike object, not list" error.

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}

Traceback:

---------------------------------------------------------------------------
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

Upvotes: 0

Views: 8558

Answers (1)

slothrop
slothrop

Reputation: 5418

The problem is that you get image_files by calling glob, so image_files is a list of paths. That causes a problem in the next line of code, because you try to do os.path.join(image_files, "*.tiff"), and the arguments to os.path.join should be strings, not lists.

If you want to get all the .tiff files in ./input/hubmap-organ-segmentation/train_images and all its descendants, then you could remove image_files and get the file list in one call:

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

Does that work?

Upvotes: 1

Related Questions