#读取一个文件夹下的所有文件的名字
import os
namelist=[]
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")
namelist.append(name)
break
def Test():
dir="H:\\json" #要读的地址
outfile="binaries.txt" #将文件名写入txt文件
wildcard = ".json" #要读取的文件类型 #".txt .exe .dll .lib .json" ,更改引号中的.json即可,也可以写多个类型,用空格隔开
file = open(outfile,"w")
if not file:
print ("cannot open the file %s for writing" % outfile)
ListFilesToTxt(dir,file,wildcard, 1)
file.close()
Test()
print namelist
读取一个文件夹下的所有文件的名字
最新推荐文章于 2024-06-26 13:30:01 发布