import os
import re
images_path = '/home/lin/'
txt_save_path = '/home/lin/name.txt'
fw = open(txt_save_path, "w")
def read_directory(directory_name,ext):
for filename in os.listdir(directory_name):
if filename.endswith(ext):
#print(filename)
fw.write(filename[:-4] + '\n')
#第一个参数是文件夹地址,第二个参数ext为你需要的文件类型,如jpg、png
read_directory(images_path,'.jpg')
参考: