Fix: Remove the following line: #label = int(each_path.split('/')[-2]) Add the following two lines: temp=each_path.split('\\')[0] label=int(temp.split('/')[3])