更改文件夹中文件的路径

时间:2017-05-14 12:11:04

标签: python python-2.7

file_path = path.relpath(file_path_images)
with open(file_path, 'r') as f:
    for line in f:
        splitLine = line.split(' ')
        ls.append(splitLine[1].strip())
        img.append(splitLine[0])

何时

print(img[0:10])

我明白了:

['./2425/1/115_Lube_45484.jpg',
 './2425/1/114_Spencerian_73323.jpg',
 './2425/1/113_accommodatingly_613.jpg',
 './2425/1/112_CARPENTER_11682.jpg',
 './2425/1/111_REGURGITATING_64100.jpg',
 './2425/1/110_savannas_67969.jpg',
 './2425/1/109_unfix_82473.jpg',
 './2425/1/108_CAGOULES_10765.jpg',
 './2425/1/107_TRANSITS_80259.jpg',
 './2425/1/106_looped_45230.jpg']

现在我想阅读我的图片:

for image in img:
     image=cv2.imread(image)

但是我需要更新路径并将'.'替换为'/home/Downloads/mnt/ramdisk/max/90kDICT32px'以获取如下路径:

'/home/Downloads/mnt/ramdisk/max/90kDICT32px/2425/1/115_Lube_45484.jpg'

1 个答案:

答案 0 :(得分:1)

import os
abspaths = [os.path.join('/home/Downloads/mnt/ramdisk/max/90kDICT32px',relpath) for relpath in img]
for image in abspaths:
     image=cv2.imread(image)