搜索某文件夹下txt文件中的内容,找出含有搜索内容的每一行
import os
def eachFile(filepath):
pathDir =os.listdir(filepath) #遍历文件夹中的text
return pathDir
def readfile(name):
fopen=open(name,'r')
for lines in fopen.readlines(): #按行读取text中的内容
lines = lines.replace("\n", "").split(",")
if 'So when we talk about a physical system' in str(lines):
#筛选出含有''中内容的每一行
print(lines)
print(name)
fopen.close()
filePath = r"C:\Users\Desktop\subtitles"
# 文件路径
pathDir=eachFile(filePath)
for allDir in pathDir:
# child = os.path.join('%s%s' % (filepath, allDir))
child = filePath + '\\' + allDir
readfile(child)