python读取文件夹下的jpg图片保存到txt

# -*- coding: utf-8 -*-
#将文件夹中图像的名字放在一个txt文件中
# 不知道为什么效果不是很好,到是也可以放进去,但是顺序不对
 
# P01 批量读取文件名,并将读取的文件名保存到指定路径下的txt中(带.*** 后缀)
 
import os
def ListFilesToTxt(dir, file, wildcard, recursion):
    exts = wildcard.split(" ")
    files = os.listdir(dir)
    for name in files:
        fullname = os.path.join(dir, name)
        if (os.path.isdir(fullname) & recursion):
            ListFilesToTxt(fullname, file, wildcard, recursion)
        else:
            for ext in exts:
                if (name.endswith(ext)):
                    file.write(name + "\n")
                    break
 
def ReadName():
    dir = "背景图"  # 读取文件路径
    outfile = "imgname.txt"                     # 将文件名写入Image.txt
    wildcard = ".jpg" # 读取jpg图片
    #   wildcard = ".jpg .txt .exe .dll .lib"      #要读取的文件类型;
    file = open(outfile, "w")
    if not file:
        print("cannot open the file %s for writing" % outfile)
    ListFilesToTxt(dir, file, wildcard, 1)
    file.close()
 
ReadName()
 

你可能感兴趣的:(python)