|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import os
- def search(start_path,keywords):
- os.getcwd()
- namelst = os.listdir(os.curdir)
- for each_file in namelst:
- if os.path.isfile:
- if os.path.splitext(each_file)[1] == '.txt':
- f = open(each_file)
- count = 0
- if keywords in f.read():
- for eachline in f:
- count += 1
- address = eachline.index(keywords)
- print('关键字出现在第%d行,第【%d】个位置n'%(count,address))
- print('========================================================')
- f.close()
- if os.path.isdir:
- search(each_file,keywords)
- os.chdir(os.pardir) #返回上一层
- keywords = input('请将该脚本放于带查找的文件夹内,请输入关键字:')
- start_path = os.getcwd()
- search(start_path,keywords)
复制代码
在运行测试的时候,返回
‘‘RecursionError: maximum recursion depth exceeded in comparison’‘
网上查询,有人说是递归层数太多,但我有返回上一层啊
15行改成:
- f = open(each_file, encoding = "utf-8")
复制代码
还有11行,函数也没调用
|
|