【python】保存某个文件夹下所有图片名字到一个txt文件里

用python实现读取某一文件夹下的所有图片名字到一个txt文件

import os
dir1='/data/Datasets/JPEGImages'#图片文件存放地址
txt1 = '/data/Datasets/picture.txt'#图片文件名存放txt文件地址
f1 = open(txt1,'a')#打开文件流
for filename in os.listdir(dir1):
    f1.write(filename.rstrip('.jpg'))#只保存名字,去除后缀.jpg
    f1.write("\n")#换行
f1.close()#关闭文件流

猜你喜欢

转载自blog.csdn.net/m0_37615398/article/details/85051489