import os
import shutil
search_string = "特定字符串"
folder_path = "文件夹路径"
output_path = "汇总文件路径"
matched_files = []
for filename in os.listdir(folder_path):
with open(os.path.join(folder_path, filename), "r") as file:
if search_string in file.read():
matched_files.append(filename)
with open(output_path, 'w') as output_file:
for file in matched_files:
with open(os.path.join(folder_path, file), "r") as input_file:
copying = False
for line in input_file:
if search_string in line:
copying = True
if copying:
output_file.write(line)
这样就可以在汇总文件中查找特定字符串并查看其后续的记录了。