鱼C论坛

 找回密码
 立即注册
查看: 2142|回复: 7

[已解决]请问代码为啥检测不出子文件夹里的文本。。

[复制链接]
发表于 2021-6-26 17:06:14 | 显示全部楼层 |阅读模式

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

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

x
  1. import os

  2. def test1(path,target):
  3.     os.chdir(path)
  4.     list1 = os.listdir('.')
  5.     for each in list1:
  6.         tuple1 = os.path.splitext(each)
  7.         if not os.path.isdir(each) and tuple1[1] =='.txt':
  8.             file_dict = dict()  # 创建空的字典
  9.             f = open(os.path.join(path,each),encoding='utf-8')
  10.             count1 = 0 #行数
  11.             for file_content in f:
  12.                 list2 = []
  13.                 count1 += 1
  14.                 index1 = file_content.find(target)
  15.                 while index1 != -1:
  16.                     list2.append(index1)   #改用列表装位置
  17.                     index1 = file_content.find(target,index1+1)
  18.                 if list2 != []:
  19.                     file_dict[count1] = list2
  20.             f.close()
  21.             if file_dict != dict():
  22.                 keys = file_dict.keys()
  23.                 keys = sorted(keys)
  24.                 print('在文件[%s]发现关键字[%s]' %(os.path.join(path,each),target))
  25.                 for file_keys in keys:
  26.                     print ('在文件第%d行,第【%s】位置出现'%(file_keys,str(file_dict[file_keys])))
  27.             elif os.path.isdir(each):
  28.                 test1(os.path.join(path,each),target)
  29.                 os.chdir('..')
  30. path = input('输入查询的初始路径:')
  31. target = input('输入查询的关键字:')
  32. test1(path,target)
复制代码
最佳答案
2021-6-26 17:22:37
杨东明 发表于 2021-6-26 17:15
elif缩进错行了。。。



除了 elif 缩进 还有个小问题,建议改成这样, open 中不用打开完整路径,否则填 . 时会报错

  1. import os

  2. def test1(path,target):
  3.     os.chdir(path)
  4.     list1 = os.listdir('.')
  5.     for each in list1:
  6.         tuple1 = os.path.splitext(each)
  7.         if not os.path.isdir(each) and tuple1[1] =='.txt':
  8.             file_dict = dict()  # 创建空的字典
  9.             f = open(each,encoding='utf-8')
  10.             count1 = 0 #行数
  11.             for file_content in f:
  12.                 list2 = []
  13.                 count1 += 1
  14.                 index1 = file_content.find(target)
  15.                 while index1 != -1:
  16.                     list2.append(index1)   #改用列表装位置
  17.                     index1 = file_content.find(target,index1+1)
  18.                 if list2 != []:
  19.                     file_dict[count1] = list2
  20.             f.close()
  21.             if file_dict != dict():
  22.                 keys = file_dict.keys()
  23.                 keys = sorted(keys)
  24.                 print('在文件[%s]发现关键字[%s]' %(os.path.join(path,each),target))
  25.                 for file_keys in keys:
  26.                     print ('在文件第%d行,第【%s】位置出现'%(file_keys,str(file_dict[file_keys])))
  27.         elif os.path.isdir(each):
  28.             test1(os.path.join(path,each),target)
  29.             os.chdir('..')
  30. path = input('输入查询的初始路径:')
  31. target = input('输入查询的关键字:')
  32. test1(path,target)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-26 17:08:15 | 显示全部楼层
我输入path为D:\\QQ下载,这个目录下我创建了一个文本,输入了关键字并保存,检测出来了,D:\\QQ下载\\123,这个子目录下我也创建了一个一样的文本,但是代码检测不到,也不报错,请问啥原因这是。。?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-26 17:08:47 | 显示全部楼层
题目是:编写一个程序,用户输入关键字,查找当前文件夹内(如果当前文件夹内包含文件夹,则进入文件夹继续搜索)所有含有该关键字的文本文件(.txt后缀),要求显示该文件所在的位置以及关键字在文件中的具体位置(第几行第几个字符)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-26 17:15:27 | 显示全部楼层
elif缩进错行了。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-26 17:17:08 | 显示全部楼层
不能自己采纳自己啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-26 17:18:13 | 显示全部楼层
代码一多就容易脑子乱
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-26 17:22:37 | 显示全部楼层    本楼为最佳答案   
杨东明 发表于 2021-6-26 17:15
elif缩进错行了。。。



除了 elif 缩进 还有个小问题,建议改成这样, open 中不用打开完整路径,否则填 . 时会报错

  1. import os

  2. def test1(path,target):
  3.     os.chdir(path)
  4.     list1 = os.listdir('.')
  5.     for each in list1:
  6.         tuple1 = os.path.splitext(each)
  7.         if not os.path.isdir(each) and tuple1[1] =='.txt':
  8.             file_dict = dict()  # 创建空的字典
  9.             f = open(each,encoding='utf-8')
  10.             count1 = 0 #行数
  11.             for file_content in f:
  12.                 list2 = []
  13.                 count1 += 1
  14.                 index1 = file_content.find(target)
  15.                 while index1 != -1:
  16.                     list2.append(index1)   #改用列表装位置
  17.                     index1 = file_content.find(target,index1+1)
  18.                 if list2 != []:
  19.                     file_dict[count1] = list2
  20.             f.close()
  21.             if file_dict != dict():
  22.                 keys = file_dict.keys()
  23.                 keys = sorted(keys)
  24.                 print('在文件[%s]发现关键字[%s]' %(os.path.join(path,each),target))
  25.                 for file_keys in keys:
  26.                     print ('在文件第%d行,第【%s】位置出现'%(file_keys,str(file_dict[file_keys])))
  27.         elif os.path.isdir(each):
  28.             test1(os.path.join(path,each),target)
  29.             os.chdir('..')
  30. path = input('输入查询的初始路径:')
  31. target = input('输入查询的关键字:')
  32. test1(path,target)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-27 09:12:29 | 显示全部楼层
Twilight6 发表于 2021-6-26 17:22
除了 elif 缩进 还有个小问题,建议改成这样, open 中不用打开完整路径,否则填 . 时会报错

谢谢谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 06:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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