鱼C论坛

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

Python第30讲课后第四题求解答

[复制链接]
发表于 2018-2-11 22:48:49 | 显示全部楼层 |阅读模式

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

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

x
  1. def search_file(start_dir,target): #建立一个搜索指定格式文件的模块
  2.     os.chdir(start_dir)
  3.     for each_file in os.listdir(start_dir):
  4.         ext = os.path.splitext(each_file)[1]
  5.         if ext in target:
  6.             text_file.append(os.getcwd + os.sep + each_file) #该列表内含有的均为路径
  7.         if os.path.isdir(each_file):
  8.             search_file(start_dir,target) #递归调用
  9.             os.chdir(os.pardir)

  10. def search_right_file(text_file): #筛选出含有关键字的文件
  11.     choice_file = []
  12.     for each_file in text_file:
  13.         choice_file.append(open(each_file,'r'))
  14.         if keyword in choice_file:
  15.             print('在文件【',each_file,'】中找到关键字【',keyword,'】')
  16.             if judge_print == 'yes': #显示关键字在文件中的具体位置
  17.                 count_line = 1
  18.                 count_colunm = []
  19.                 for each_line in choice_file:
  20.                     if keyword in each_line:
  21.                         for i in len(each_line):
  22.                             if each_line[i] == 'keyword[0]' and each_line[i+1] == 'keyword[1]':
  23.                                 count_colunm,append(i)
  24.                     print('关键字出现在第',count_line,'行,','第',count_colunm,'个位置')
  25.                     count_line +=1
  26.                     count_colunm = []
  27.             choice_file = []
  28.         else:
  29.             text_file.remove(each_file)
  30.             choice_file = []
  31.             continue


  32. import os
  33. target = ['txt']
  34. text_file = []
  35. start_dir = input('请输入待查找的初始目录:')
  36. keyword = input('请输入关键词:')
  37. judge_print = input('请问是否需要打印关键字在文件中的具体位置(yes/no):')
  38. search_file(start_dir,target)
  39. search_right_file(text_file)
复制代码

运行后我也不知道是什么错误,求解答!
微信图片_20180211224634.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-2-12 00:03:30 | 显示全部楼层
这是从哪捡来的代码,全是问题
search_file 帮你更正。其它的自己看吧。
def search_file(start_dir,target): #建立一个搜索指定格式文件的模块
    os.chdir(start_dir)
    for each_file in os.listdir(start_dir):
        ext = os.path.splitext(each_file)[1]
        if ext in target:
            text_file.append(os.getcwd() + os.sep + each_file) #该列表内含有的均为路径
        if os.path.isdir(each_file):
            search_file(os.getcwd() + os.sep + each_file,target) #递归调用
            os.chdir(os.pardir)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-26 21:52:06 | 显示全部楼层
第八行  提示 递归错误那个
search_file(start_dir,target) #递归调用
search_file(each_file,target) #递归调用 换成for循环里面的才是
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-10 22:47:33 | 显示全部楼层

                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-30 05:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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