python读取文件夹路径下所有图片并保存在txt文件以及读取操作

记录一下个人学习过程
写入

import os

readpath = 'data/imgs' # 文件夹位置

files = os.listdir(readpath) # 读取文件夹下文件名

f = open(readpath + '/' + 'list.txt', 'w')# 创建文本文件

for file in files: # 便利所以文件名
    if any(file.endswith(extension) #判断是不是图片
           for extension in ['.tif', '.png', '.jpg', '.jpeg', '.PNG', '.JPG', '.JPEG']):
        f.write(readpath + '/' + file + '\n') # 写入文件
f.close()

读取

import os

readtxtpath = 'data/imgs/list.txt' # 文件夹位置
f = open(readtxtpath , "r")
lines = f.readlines()
lines = [line.strip("\n") for line in lines] # 去除/n
f.close()

你可能感兴趣的:(python图像处理基础操作,python,图像处理)