#!/usr/bin/env python3
import os
dirpath = '/home/zyy/汽车/卡槽'
targetfile = 'imagepath.txt'
def isimage(fn):
return os.path.splitext(fn)[-1] in (
'.jpg', '.JPG', '.png', '.PNG')
def main():
imagelist = []
for r, ds, fs in os.walk(dirpath):
for fn in fs:
if not isimage(fn):
continue
fname = os.path.join(r, fn)
print(fname)
imagelist.append(fname)
if not imagelist:
print('image not found')
return
target = os.path.join(dirpath, targetfile)
with open(target, 'w') as f:
f.write('\n'.join(imagelist))
print('the path of images have been wirte to',
target)
if __name__ == '__main__':
main()