文件系统
编写一个程序,用户输入关键字,查找当前文件夹内(如果当前文件夹内包含文件夹,则进入文件夹继续搜索)所有含有该关键字的文本文件(.txt后缀),要求显示该文件所在的位置以及关键字在文件中的具体位置(第几行第几个字符),程序实现如图:import os
def print_pos(key_dict):
keys = key_dict.keys()
keys = sorted(keys)
for each_key in keys:
print("关键字出现在第%行,第%s个位置。" % (each_key,str(key_dict)))
def pos_in_line(line,key):
pos = []
begin = line.find(key)
while begin != -1:
pos.append(begin + 1)
begin = line.find(key,begin+1)
return pos
def search_in_file(file_name,key):
f = open(file_name)
count = 0
key_dict = dict()
for each_line in f:
count += 1
if key in each_line:
pos = pos_in_line(each_line,key)
key_dict = pos
def search_files(key,detail):
all_files = os.walk(os.getcwd())
txt_files = []
for i in all_files:
for each_file in i:
if os.path.splitext(each_file) == '.txt':
each_file = os.path.join(i,each_file)
txt_files.append(each_file)
for each_txt_file in txt_files:
key_dict = search_in_file(each_txt_file,key)
if key_dict:
print('============================================================')
print('在文件【%s】中找到关键字【%s】' % (each_txt_file,key))
if detail in ['YES','yes','Yes']:
print_pos(key_dict)
key = input('请将该脚本放于待查找的文件内,请输入关键字:')
detail = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' % key)
search_files(key,detail)
这是零基础学PYTHON系列第30课后最后一题课后习题,我把测试文件放在了保存该代码文件的同一个文件夹内,运行以后没有结果,这是为什么呢?
帮你改完了,search_in_file 函数忘记返回 key_dict 字典了
而而且 search_in_file 函数 的 key_dict = pos,应该加一个缩进到 if 下
参考代码:
import os
def print_pos(key_dict):
keys = key_dict.keys()
keys = sorted(keys)
for each_key in keys:
print("关键字出现在第%d行,第%s个位置。" % (each_key, str(key_dict)))
def pos_in_line(line, key):
pos = []
begin = line.find(key)
while begin != -1:
pos.append(begin + 1)
begin = line.find(key, begin + 1)
return pos
def search_in_file(file_name, key):
f = open(file_name)
count = 0
key_dict = dict()
for each_line in f:
count += 1
if key in each_line:
pos = pos_in_line(each_line, key)
key_dict = pos
return key_dict
def search_files(key, detail):
all_files = os.walk(os.getcwd())
txt_files = []
for i in all_files:
for each_file in i:
if os.path.splitext(each_file) == '.txt':
each_file = os.path.join(i, each_file)
txt_files.append(each_file)
for each_txt_file in txt_files:
key_dict = search_in_file(each_txt_file, key)
if key_dict:
print('============================================================')
print('在文件【%s】中找到关键字【%s】' % (each_txt_file, key))
if detail in ['YES', 'yes', 'Yes']:
print_pos(key_dict)
key = input('请将该脚本放于待查找的文件内,请输入关键字:')
detail = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' % key)
search_files(key, detail) Twilight6 发表于 2020-8-12 14:07
帮你改完了,search_in_file 函数忘记返回 key_dict 字典了
而而且 search_in_file 函数 的 key_dict[ ...
大佬,这Python是怎么学的,我这跟着小甲鱼老师视频学了半个月,那作业得看答案才有点思路怎么办啊?{:10_266:} 本帖最后由 yhhpf 于 2020-8-12 13:41 编辑
1,代码问题上你对照这答案看下吧,你发出来的缩进上是有问题的,如何正确的发代码请参考:https://fishc.com.cn/thread-128631-1-1.html
2,课程中没有提到文件编码的问题,你看下你要查找的文件的编码格式,确定后需要做修改,例如确认是utf-8格式的:
f = open(file_name)
需要改为:
f = open(file_name,encoding='utf-8')
3,编码问题可以看下我以前给的回答:https://fishc.com.cn/forum.php?mod=viewthread&tid=173329&highlight=30%BF%CE
Twilight6 发表于 2020-8-12 14:07
帮你改完了,search_in_file 函数忘记返回 key_dict 字典了
而而且 search_in_file 函数 的 key_dict[ ...
谢谢大佬{:5_105:}
页:
[1]