Home > database >  IndexError: list index out of range when using split in glob
IndexError: list index out of range when using split in glob

Time:12-19

I am getting the below error when I try to train a model on my local Windows PC. This error does not happen in Colab, so I think it's something related to the path or Linux.

I get the list out of index error when I try to call label = labels_to_number(TRAIN_PATH) in my main file

The snippet of the main file:

TRAIN_PATH = './data/train/'
.
.
.
.

labels = labels_to_number(TRAIN_PATH) #transform labels from string to number

The data_utils.py file:

def labels_to_number(path):
   
    classes = [i.split(os.path.sep)[3] for i in glob.glob(path   '*')]
    classes.sort()

    labels_dict = {}
    for i, label in enumerate(classes):
        labels_dict[label] = i

    return labels_dict

The error I get:

Traceback (most recent call last):
  File "E:\Project\train_model.py", line 28, in <module>
    labels = labels_to_number(TRAIN_PATH)
  File "E:\Project\data_utils.py", line 12, in labels_to_number
    classes = [i.split(os.path.sep)[3] for i in glob.glob(path   '*')]
  File "E:\Project\data_utils.py", line 12, in <listcomp>
    classes = [i.split(os.path.sep)[3] for i in glob.glob(path   '*')]
IndexError: list index out of range

Can someone please help fix this problem ? Thank you.

CodePudding user response:

Given your input: TRAIN_PATH = './data/train/' your os.path.sep appears three times, but in Python the index is counted starting at 0

So your split input should be this:

# TRAIN_PATH = './data/train/'
classes = [i.split(os.path.sep)[2] for i in glob.glob(path   '*')]

CodePudding user response:

Since you supplied a relative data path the list generated from i.split(os.path.sep) will have less elements and that's why it throws an error

  • Related