我正在编写一个Python 3.7.2脚本,该脚本使用re.compile搜索目录中的所有文件,并返回出现特定字符串的每一行。除以下两个问题外,它工作正常:
有什么建议吗?这是脚本:
import os
import re
err_occur = []
pattern = re.compile(r"An important phrase")
path = "/users/mypath/"
for filename in os.listdir(path):
try:
with open(os.path.join(path, filename), encoding="UTF-8") as in_file:
for linenum, line in enumerate(in_file):
if pattern.search(line) != None:
err_occur.append((linenum, line.rstrip('\n')))
for linenum, line in err_occur:
print(line, sep='')
except FileNotFoundError:
print("Input file not found.")