为什么使用os.listdir访问时我的目录总是更改?

时间:2019-05-12 16:37:20

标签: python file

我想访问文件的目录,但事实证明该目录提供了两个输出,即当前路径和前一个路径。但是,它将访问先前的路径。例如:

这些是我想要的目录:

train/shape/ball 
train/shape/square

...但是这些是我得到的目录:     训练/形状/球#我想要什么     火车/#我得到了什么

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

我尝试使用os.listdir进行拆分,但它仍可以访问先前的目录:

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 个答案:

没有答案