|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #!/usr/bin/env python
- #_*_ coding:utf-8 _*_
- '''
- 在指定目录中查找包含关键字的文件
- Version 1.0
- Author xpli
- '''
- import os
- def print_key_position_detail(key_dict):
- keys = key_dict.keys()
- keys = sorted(keys)
- for each_key in keys:
- print("关键字出现在第%d行,第%s个位置。"% (each_key, str(key_dict[each_key])))
- def search_key_line(line, key):
- position = []
- begin = line.find(key)
- while begin != -1:
- position.append(begin + 1)
- begin = line.find(key, begin+1)
- return position
- def search_key_doc(doc_name, key):
- count = 0
- f = open(doc_name,"r", encoding = "ascii")
- key_dict = dict()
- for each_line in f:
- count += 1
- pos = search_key_line(each_line, key)
- key_dict[count]= pos
- return key_dict
- f.close()
- def search_file(file_path, key, detail):
- os.chdir(file_path)
- listdir = os.listdir(os.curdir)
- doclist = []
- for each_file in listdir:
- if each_file == "System Volume Information":
- continue
- if os.path.isdir(each_file):
- search_file(each_file, key, detail)
- os.chdir(os.pardir)
- if os.path.splitext(each_file)[1] == ".txt":
- doc_position = os.getcwd() + os.sep + each_file
- doclist.append(doc_position)
- for each in doclist:
- key_dict = search_key_doc(each, key)
- if key_dict:
- print ("=========================================")
- print ("在文件【%s】中找到关键字【%s】" % (each, key))
- if detail in ["YES", "yes", "Yes"]:
- print_key_position_detail(key_dict)
- file_path = input("请输入要查找关键字的路径:")
- key = input("请输入要查找的关键字:")
- detail = input("请确认是否需要打印关键字【%s】所在位置的详细信息(YES/NO):" % key)
- search_file(file_path, key, detail)
-
复制代码
执行上面这段代码总是解决错误。查找了半天百度也没有解决,求助各位同学帮忙解决下:
问题如下:
Current directory: F:\
请将该脚本放于待查找的文件夹内,请输入关键字:新版本
请问是否需要打印关键字【新版本】在文件中的具体位置(YES/NO):yes
Traceback (most recent call last):
File "new 1.py", line 56, in <module>
search_files(key, detail)
File "new 1.py", line 46, in search_files
key_dict = search_in_file(each_txt_file, key)
File "new 1.py", line 25, in search_in_file
for each_line in f:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 8: illegal multibyte sequence |
|