import codecs def loaddata(): kfFile = codecs.open(r'D:\untitled\jiequkaifang.txt','rb',encoding='gbk',errors='ignore') global namelist namelist = kfFile.readlines() kfFile.close() def search(name): searchfile = codecs.open('D:\\untitled\\'+name+'.txt','w',encoding='utf-8') a = 0 for line in namelist: if line.find(name)!=-1: print(line) searchfile.write(line) a +=1 searchfile.write('所有名称为{}的共{}个'.format(name,str(a))) searchfile.close() namelist = [] loaddata() inputname = input("input:") search(inputname)