读取文件中的所有图片的路径,保存到txt文件中。

2022-05-09 14:15:01 浏览数 (1)

代码语言:javascript复制
import sys
import os.path

if __name__ == "__main__":

    f = open('dataset.txt', 'w')
    # 文件名,文件下还有多个类别的文件。
    BASE_PATH="JAFFE"
    SEPARATOR=";" 
    # 绝对路径地址
    pth = "/Users/liupeng/Desktop/my/faceER/DataSet/"

    for dirname, dirnames, filenames in os.walk(BASE_PATH):
        for subdirname in dirnames:
            label = subdirname
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                abs_path = "%s/%s" % (subject_path, filename)
                abs_path = pth   abs_path
                print "%s%s%s" % (abs_path, SEPARATOR, label)
                f.write("%s%s%sn"%(abs_path, SEPARATOR, label))
    f.close();
txt

0 人点赞