import os import re

path = input("请输入文件夹路径:") regex = input("请输入正则表达式:")

for file_name in os.listdir(path): if file_name.endswith(".txt"): file_path = os.path.join(path, file_name) with open(file_path, "r") as file: for line in file: if re.search(regex, line): print(line.strip())

编写一个程序以打开文件夹中所有的txt文件并查找匹配用户提供的正则表达式的所有行。结果应该输出到屏幕上。

原文地址: https://www.cveoy.top/t/topic/cjkh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录