本帖最后由 txxcat 于 2020-4-24 01:58 编辑
你的代码还有几处错误,看注释:import os
paths = [] # 存放有关键字的文件的路径
each_txt = [] # 存放每个文件中每个关键字{行:列}字典的列表
def copen(filename):
try:
f = open(filename,encoding='GBK')
try:
f.read()
f.seek(0)
except UnicodeDecodeError:
f = open(filename,encoding='utf-8',errors='ignore')
except (OSError,AttributeError) as reason:
print(str(reason))
return None
return f
def search_keyword(keyword) :
all_files = os.listdir(os.curdir)
for each_file in all_files:
ext = os.path.splitext(each_file)[1]
if os.path.isdir(each_file) :
os.chdir(each_file)
search_keyword(keyword)
os.chdir(os.pardir)
if ext == '.txt':
path = os.getcwd() + os.sep + each_file
f = copen(each_file) #用函数copen打开防止编码错误
line_site = dict()
line_num = 0
for each_line in f:
site_num = []
line_num += 1
i = 0
while 1:
site = each_line.find(keyword, i)
if site != -1:
site_num.append(site+1) #python索引从0开始,人类习惯1开始
i = site + 1
else:
break
if site_num == []:
continue
else:
line_site[line_num] = site_num
# each_txt.append(line_site) #在这里append,每找到一行就append一次,重复数据导致错误
if line_site == {}:
continue
else:
paths.append(path)
each_txt.append(line_site) #在这里append就不会有重复数据
f.close()
keyword = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
search_keyword(keyword)
decide = input('请问是否需要打印关键字【%s】在文件中大的具体位置(YES/NO):' % keyword)
if decide.strip().lower()=='yes': #这样可以对付任意大小写组合,还有空格
if paths == []:
print('===================================================')
print('当前文件夹内所有文本文件均不含有此关键字!')
else:
print('===================================================')
for i in range(len(paths)): #原有的代码是有多少符合条件的文件,就把所有的答案重复打印多少次
print('在文件【%s】中找到关键字【%s】' % (paths[i], keyword)) #只打印和文件对应的字典
for j,k in each_txt[i].items():
print('关键字出现在第 %d 行,第%s个位置。' % (j, k))
# for each_path in paths: # paths有一个元素就说明有一个文件有关键字
# print('在文件【%s】中找到关键字【%s】' % (each_path, keyword))
#
# for each_line_site in each_txt: # each_txt是一个列表(每个文件一个列表),每个元素each_line_site都为字典
#
# for each_item in each_line_site.items(): # each_item是字典的每一项,是一个元组(键(行),值(列))
# print('关键字出现在第 %d 行,第' % each_item[0], each_item[1], '个位置。') # 值(列)是一个列表
|