|
发表于 2021-3-7 11:16:03
|
显示全部楼层
- import os
- import os.path
- def search(path,key):
- os.chdir(path)
- filelist=os.listdir()
- for each in filelist:
- if os.path.splitext(each)[1]=='.txt':
- f=open(os.getcwd()+'\\'+each,encoding='utf-8')
- line=0
- for each_line in f:
- line+=1
- if key in each_line:
- position=[]
- for eachkey in key:
- posision.append(each_line.find(eachkey))
- print('关键字出现在第'+str(line)+'第'+position+'位置')
- f.close()
- elif os.path.isdir(each)==1:
- path=os.getcwd()+'\\'+each
- search(path,key)
- os.chdir('..')
- path=input('请输入路径:')
- key=input('请输入关键字:')
- search(path,key)
复制代码 |
|