鱼C论坛

 找回密码
 立即注册
查看: 866|回复: 3

[已解决]为什么递归失败?

[复制链接]
发表于 2020-4-4 20:38:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import os

  2. def key(word):
  3.     def pos(each_line,word):
  4.         line_pos = []
  5.         begin = line.find(word)
  6.         while begin != -1:
  7.             line_pos.append(begin + 1)
  8.             begin = each_line.find(key,begin+1)
  9.             print(1)
  10.     def open_file(each_file):
  11.         global file_way
  12.         print(2)
  13.         f = open(file_way)
  14.         count_line = 0
  15.         for each_line in f:
  16.             count_line += 1
  17.             if word in each_line: #开始索引
  18.                 pos(each_line,word)
  19.                 print('关键字出现在%d行,第[%s]个位置' %(count_line,str(line_pos)))
  20.         f.close()
  21.     def y(word):
  22.         for each_file in os.listdir(os.curdir):
  23.             print(3)
  24.             outreach = os.path.splitext(each_file)[1]
  25.             if outreach == 'txt':   #先判断文件扩展名是否为txt文件
  26.                 file_way = os.curdir + os.sep + each_file
  27.                 open_file(each_file) #打开函数
  28.             if os.path.isdir(each_file):
  29.                 y(each_file)
  30.                 os.chdir(os.pardir)
  31.     discuss = input('请问是否需要打印关键字【%s】在文件中的具体文职(YES/NO):')
  32.     if discuss == 'YES' or discuss == 'yes':
  33.         y(word)
  34. word = input('请将该脚本放于待查找的文件夹内,请输入关键字:')

  35. key(word)
复制代码



NJVU]PJD1Q51RAXDL2UZP_1.png
最佳答案
2020-4-4 21:01:10
本帖最后由 dlnb526 于 2020-4-4 21:04 编辑

emmm首先,你的缩进都是用空格的呀~~调试好费劲。建议下次用tab键~~然后,
  1.     def y(word):

  2.         for each_file in os.listdir(os.curdir):
  3.             outreach = os.path.splitext(each_file)[1]
  4.             if outreach == 'txt':   #先判断文件扩展名是否为txt文件
  5.                 file_way = os.curdir + os.sep + each_file
  6.                 open_file(each_file) #打开函数
  7.             if os.path.isdir(each_file):
  8.                 print(each_file)
  9.                 y(each_file)
  10.                 os.chdir(os.pardir)
  11.     discuss = input('请问是否需要打印关键字【%s】在文件中的具体文职(YES/NO):')
  12.     if discuss == 'YES' or discuss == 'yes':
  13.         y(word)
复制代码


如上通过打印调试
是这里没有迭代出,一直在第一个文件夹循环呢~重新检查迭代条件吧

问题在于你传入的word参数没有用~~~emmm所以自然一直在这一层迭代了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-4 20:52:24 | 显示全部楼层
递归层数太多了,超出了系统的设定……
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 21:01:10 | 显示全部楼层    本楼为最佳答案   
本帖最后由 dlnb526 于 2020-4-4 21:04 编辑

emmm首先,你的缩进都是用空格的呀~~调试好费劲。建议下次用tab键~~然后,
  1.     def y(word):

  2.         for each_file in os.listdir(os.curdir):
  3.             outreach = os.path.splitext(each_file)[1]
  4.             if outreach == 'txt':   #先判断文件扩展名是否为txt文件
  5.                 file_way = os.curdir + os.sep + each_file
  6.                 open_file(each_file) #打开函数
  7.             if os.path.isdir(each_file):
  8.                 print(each_file)
  9.                 y(each_file)
  10.                 os.chdir(os.pardir)
  11.     discuss = input('请问是否需要打印关键字【%s】在文件中的具体文职(YES/NO):')
  12.     if discuss == 'YES' or discuss == 'yes':
  13.         y(word)
复制代码


如上通过打印调试
是这里没有迭代出,一直在第一个文件夹循环呢~重新检查迭代条件吧

问题在于你传入的word参数没有用~~~emmm所以自然一直在这一层迭代了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-4 21:54:19 | 显示全部楼层
dlnb526 发表于 2020-4-4 21:01
emmm首先,你的缩进都是用空格的呀~~调试好费劲。建议下次用tab键~~然后,

好吧,txt前面少了一个点
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-20 01:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表