python实现读取目录文件夹下的所有指定格式文件路径

import os

def get_img_file(file_name):
    imagelist = []
    for parent, dirnames, filenames in os.walk(file_name):
        for filename in filenames:
            if filename.lower().endswith(('.txt')):
                imagelist.append(os.path.join(parent, filename))
        return imagelist

if __name__ == '__main__':
    file_name="/home/dataset/test/labels/"
    imagelist = []
    imagelist = get_img_file(file_name)

    f=open("read_rs.txt","w")
    if imagelist is not None:
        for image_filename in imagelist:
            f.write(image_filename+'\n')
            print(image_filename)
    f.close()
    

你可能感兴趣的:(python)