不确定.join()如何使用两个参数

时间:2018-02-21 01:43:54

标签: python python-3.x join

在以下方法中,我不确定 .join(data_directory,d) .join(ROOT_PATH," TrafficSigns / Training")收益率。具体来说,我不知道.join()如何处理这两个参数。

def load_data(data_directory):
    directories = [d for d in os.listdir(data_directory) 
                   if os.path.isdir(os.path.join(data_directory, d))]
    labels = []
    images = []
    for d in directories:
        label_directory = os.path.join(data_directory, d)
        file_names = [os.path.join(label_directory, f) 
                      for f in os.listdir(label_directory) 
                      if f.endswith(".ppm")]
        for f in file_names:
            images.append(skimage.data.imread(f))
            labels.append(int(d))
    return images, labels

ROOT_PATH = "/your/root/path"
train_data_directory = os.path.join(ROOT_PATH, "TrafficSigns/Training")
test_data_directory = os.path.join(ROOT_PATH, "TrafficSigns/Testing")

images, labels = load_data(train_data_directory)

快速说明,我正在从TrafficSign文件夹(包含两个文件夹,Training和Testing)中加载图像。

1 个答案:

答案 0 :(得分:0)

os.path.join()

这可以将字符串安全地连接在一起。它使用os.sep来确保os平台使用正确的路径分隔符。它可以纠正额外的分隔符。

mypath = os.path.join("/some/path/", "/directory")
print(mypath) # "/some/path/directory"