'Python Error "List out of range", but the index exists

I am trying to split a file name and get the last part of it, using split() funtion.

folders = [f for f in os.listdir(TESS) if not f.startswith(".")]

emotion_file = []

for dir in folders:
    list_dir = os.listdir(TESS + "/"+ dir)
    for file in list_dir:
        part = file.split(".")[0]
        part = part.split("_")
        emotion_file.append(part[2])

The part is : ['OAF', 'rain', 'sad'], so the index 2 should exist, but I keep getting the list out of index error when I want to get the Index 2. Can anyone help me with it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source