I want to access a directory of a file, but it turns out that the directory gives two output, the current path and the previous path. But, then it will access the previous path. For example:

These are the directories that I want:

train/shape/ball 
train/shape/square

...but these are the directories that I get: train/shape/ball # WHAT I WANT train/ # WHAT I GET

train/shape/square  # WHAT I WANT
train/              # WHAT I GET

I tried using os.listdir and splitting but it still accesses the previous directory:

import os

img_path = 'train/'

f1 = open('train_file.txt','r')
train_list = f1.readlines()

'''
Here is inside some of train_list

shape/ball_0 
shape/ball_1 
shape/ball_2 
shape/ball_3 
shape/square_0 
shape/square_1 
shape/square_2 
... etc

'''

for line in train_list:
    name = line.split(' ')[0]

    image_path = img_path + name
    label = line.split(' ')[-1]

    #when I tried to print the path, it results in 2 parts as in the problems
    #print(image_path)

    '''
    image_path will have 2 outputs
    train/shape/ball    #this is the directory that I want to access
    train/
    '''

0 Answers