如果最终访问路径为以下 ‘D:/Users/RY/PycharmProjects/pythonProject15/images&labels.csv’ 我们可以采用两种方式具体如下 方法一 采用with open(image_path+filename)
import csv
import os
images, labels = [], []
image_path='D:/Users/RY/PycharmProjects/pythonProject15/'
filename='images&labels.csv'
with open(image_path+filename) as f:
reader = csv.reader(f)
for row in reader:
img, label = row
label = int(label)
images.append(img)
labels.append(label)
方法二 with open(os.path.join(image_path,filename))
import csv
import os
images, labels = [], []
image_path='D:/Users/RY/PycharmProjects/pythonProject15'
filename='images&labels.csv'
with open(os.path.join(image_path,filename)) as f:
reader = csv.reader(f)
for row in reader:
img, label = row
label = int(label)
images.append(img)
labels.append(label)
两种方式看似相同其实有小小的区别就是最后的’/‘,采用os.path.join它会自动加’/‘,所以我们再定义路径的时候要把最后的’/'去掉。
|