文件夹中所有文件名字输出到txt

# 将文件夹中所有文件名字输出到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)):
                    (filename, extension) = os.path.splitext(name)
                    file.write(name + "\n")
                    break


def Test():
    dir = "122"
    outfile = "depth.txt"
    wildcard = ".png .PNG .HEIC .jpeg .bmp .jpg .JPG .GIF .gif .tif .BMP .heic .JPEG "

    file = open(outfile, "w")
    if not file:
        print ("cannot open the file %s for writing" % outfile)
    ListFilesToTxt(dir, file, wildcard, 1)

    file.close()


Test()
本文作者:Author:     文章标题:文件夹中所有文件名字输出到txt
本文地址:https://www.ningcaichen.top/archives/91.html     
版权说明:若无注明,本文皆为“宁采晨's Blog”原创,转载请保留文章出处。
Last modification:2019 年 11 月 13 日 00 : 37 AM
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment

召唤看板娘