搜索某文件夹下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)
本文介绍了一个简单的Python脚本,该脚本可以搜索指定文件夹下的所有txt文件,并查找包含特定字符串的内容。通过逐行读取文件,筛选出含有指定字符串的行并打印出来。
1591

被折叠的 条评论
为什么被折叠?



