|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import os
- def find_repeat(source, elmt):
- elmt_index=[]
- s_index = 0;e_index = len(source)
- while(s_index < e_index):
- try:
- temp = source.index(elmt,s_index,e_index)
- elmt_index.append(temp)
- s_index = temp + 1
- except ValueError:
- break
- return elmt_index
- def Fun5(address, keywords):
- global choose
- a = list(os.listdir(address))
- for each in a:
- if os.path.isfile(address + '\\' + each):
- if (os.path.splitext(each)[1]) == ".txt":
- f = open(address + '\\' + each ,'r')
- line = 0
- context = ''
- for each_line in f:
- context += each_line
- if keywords in context:
- print("=================================================================")
- print("在文件【%s】中找到关键字【%s】"%((address + '\\' + each),keywords))
- f = open(address + '\\' + each ,'r')
- for each_line in f:
- line += 1
- if (keywords in each_line) and (choose == ("yes" or "YES" or "Yes")):
- print("关键字出现在第%d行,第%s个位置。"%(line,str(find_repeat(each_line,keywords))))
- f.close()
- elif os.path.isdir(address + '\\' + each):
- Fun5((address + '\\' + each), keywords)
-
- keywords = input("请将该代码放于带查找的文件夹内,请输入关键字:")
- choose = input("请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):"%keywords)
- address = os.path.abspath('.')
-
- Fun5(address, keywords)
复制代码
运行后显示:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence
请问是什么原因导致这个Error的?
|
-
|