|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import os
- os.getcwd()
- def pos_line(each_line,word):
- pos = []
- begin = each_line.find(word)
- while begin != -1:
- pos.append (begin+1)
- begin = each_line.find(word,begin + 1)
- return pos
- def search_line (filename,word):
- f = open (filename)
- count = 0 #记录行数
- pos_hang = dict () #字典,记录位置
- for each_line in f:
- count += 1
- if word in each_line:
- pos_hang[count] = pos_line(each_line,word)
- f.close()
- return pos_hang
-
- def search (word):
- file = os.walk (os.getcwd())
- eachfile = []
- for i in file:
- for each in i[2]:
- if os.path.splitext(each)[1] == '.txt':
- eachfile.append (os.path.join(i[0],each))
- for eachtxt in eachfile:
- key = search_line (eachtxt,word)
- keys = key.keys() #把行提出来
- keys = sorted(keys) #排序
- for each_key in keys:
- print('关键字【%s】在第%s行第%s个' % (word,each_key,str(key[keys])))
-
- f = input ('请输入关键字:')
- search (f)
复制代码
检查的文件就在代码的目录下,有txt中写了小甲鱼的,求大神指教一下,为什么没东西出来 |
|