|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
学了easygui想改写一下,大佬看看哪些地方有问题,我晕了!
import os
import easygui as g
linedict={}
posdict={}
posinline=[]
pos=[]
count=0
text_files=[]
def show_result(path,keyword):
text=''
for i in linedict:
for j in linedict[i]:
text+='在文件【%s】中找到关键字%s\n关键字出现在第%s行,第%s个位置'%(i,keyword,j,posdict[i])
msg='结果如下'
titile='统计结果'
g.textbox(msg,titile,text)
def search_line(filename,keyword):
for each_textfile in text_files:
with open(each_textfile) as f:
try:
if keyword in f.read():
for eachline in f:
count+=1
if keyword in eachline:
posinline.append(count)
search_pos(filename,eachline,keyword)
linedict[filename]=posinline
except UnicodeDecodeError:
pass
def search_pos(filename,eachline,keyword):
begin=eachline.find(keyword)
while begin!=-1:
pos.append(begin+1)
begin=eachline.find(keyword,begin+1)
posdict[filename]=pos
def search_file(path,keyword):
allfiles=os.walk(path)
for k in allfiles:
for eachfile in k[2]:
if os.path.splitext(eachfile)[1]=='.txt':
eachfile=os.path.join(k[0],eachfile)
text_files.append(eachfile)
search_line(eachfile,keyword)
keyword=g.enterbox(msg='请输入关键字')
path=g.diropenbox()
search_file(path,keyword)
show_result(path,keyword)
|
|