我不明白为什么我只在一个循环中写入的日志文件中获得 word 和 ln 的第一个匹配项(有 50 个或更多匹配项)。而且它的结构不像我打印到屏幕时那样。下面是代码。谢谢!
我正在编写的文件中的结果:343438363939 70642
regex = re.compile(r'(?:3\d){6}')
for root,dirname, files in os.walk(directory):
for file in files:
if file.endswith(".log") or file.endswith(".txt"):
f = open(os.path.join(root,file))
for i, line in enumerate(f.readlines()):
searchedstr = regex.findall(line)
ln = str(i)
for word in searchedstr:
print "\nString found: " + word
print "Line: " + ln
print "File: " + os.path.join(root,file)
print " "
logfile = open('result3.log', 'w')
logfile.write(word + '\n' + ln)
logfile.close()
f.close()