【发布时间】:2020-12-12 11:47:28
【问题描述】:
请帮忙。 我正在几个目录中搜索几个 .txt 文件以查找模式。如果有匹配,我想打印匹配的文件名和位置。
这是我的代码:
a = ('Z:/rodney/020year/2020-04/')
b = []
for y in os.listdir(a):
b.append(a+y+'/')
for filename in b:
path = filename
for filenames in listdir(path):
with open(path+filenames) as currentfile:
text = currentfile.read()
loan = re.compile(r'2 NNN \d LOANS')
bb = loan.search(text)
with open('z:/rodney/results.txt','a') as f:
f.write(os.path.dirname(path)+' ')
f.write(filenames[:-4]+'\n')
f.write(bb)
错误消息 = "TypeError: write() 参数必须是 str,而不是 None"
如果有匹配项,我只想查看匹配项的文件名和位置。我不需要在每个不匹配的文件中都看到“None”。
【问题讨论】: