python代码:读取指定文件夹内的图片并保存到txt文件中

list_txt = open(‘list.txt’, ‘w’, encoding=‘utf-8’)
import os

def gci(filepath):
files = os.listdir(filepath)
for fi in files:
fi_d = os.path.join(filepath,fi)
if os.path.isdir(fi_d):
gci(fi_d)
else:
str = os.path.join(filepath,fi_d)+’\n’
suffix = str[-5:]
print(suffix)
if(suffix == “.jpg\n”):
print(str ,‘is a jpg file’ + ‘\n’)
list_txt.writelines(str)
else:
print(str,‘is not a jpg file’ + ‘\n’)

list_txt = open(‘list.txt’, ‘w’)
path = r’/home/hh/JPEGImages’
print(path)
gci(path)
list_txt.close()

你可能感兴趣的:(python代码)