import os
def ListFilesToTxt(input_dir, file, wildcard, recursion):
exts = wildcard.split(" ")
for root, subdirs, files in os.walk(input_dir):
for name in files:
for ext in exts:
if (name.endswith(ext)):
file.write(name + "\n")
break
if (not recursion):
break
def Test():
input_dir = "pedestrian_detection/dataset/BMPImages"
outfile = "train_list.txt"
# wildcard = ".txt .exe .dll .lib"
wildcard = ".bmp"
file = open(outfile, "w")
if not file:
print("cannot open the file %s for writing" % outfile)
ListFilesToTxt(input_dir, file, wildcard, 0)
file.close()
Test()
不带文件后缀
import os
names = os.listdir('pedestrian_detection/dataset/BMPImages') # 路径
i = 0 # 用于统计文件数量是否正确,不会写到文件里
train_val = open('test.txt', 'w')
for name in names:
index = name.rfind('.')
name = name[:index]
train_val.write(name + '\n')
i = i + 1
print(i)