# -*- coding: UTF-8 -*-
# !/usr/bin/env python3
# Author:  Murphy

import os


class Searcher(object):

    """
    文档内容检索器:
        1: 功能:以内容为对象查找文件路径。
        2: 注意:仅支持能以记事本方式打开的文件,文件后缀如.txt/.js/.php/.py/.log/.config等,不支持doc文档。
        3: 其他:支持多格式查找,后缀之间请用英文逗号(,)隔开
    """

    def __init__(self):
        self.file_list = []
        self.result_list = []
        self.unknown_list = []
        self.search_path = input("请输入检索路径:  ")
        self.search_text = input("请输入检索内容:  ")
        self.filter_char = input("请输入文件后缀:  ")

    def get_file(self, file_dirs):
        for file_dir in file_dirs:
            if os.path.isdir(file_dir):
                file_dirs_temp = [file_dir + "\\" + os.listdir(file_dir)[index] for index in range(len(os.listdir(file_dir)))]

                """
                file_dirs_temp = os.listdir(file_dir)
                for index in range(len(file_dirs_temp)):
                    file_dirs_temp[index] = file_dir + "\\" + file_dirs_temp[index]
                """

                self.get_file(file_dirs_temp)
            elif os.path.exists(file_dir):
                if (os.path.splitext(file_dir)[1] in self.filter_char) or (
                        os.path.splitext(file_dir)[1][1:] in self.filter_char):
                    self.file_list.append(file_dir)
                else:
                    pass
            else:
                self.unknown_list.append(file_dir)

    def work(self):
        if not os.path.isdir(self.search_path):
            raise ValueError("路径不正确,请检查路径重新再试。")

        if not self.search_text:
            raise ValueError("检索内容不能为空。")

        if not self.filter_char:
            raise ValueError("文件后缀不能为空。")

        if "," not in self.filter_char:
            self.filter_char = [self.filter_char]
        else:
            self.filter_char = self.filter_char.split(",")

        """
        file_dirs = os.listdir(self.search_path)
        for index in range(len(file_dirs)):
            file_dirs[index] = self.search_path + "\\" + file_dirs[index]
        """

        file_dirs = [self.search_path + "\\" + os.listdir(self.search_path)[index] for index in range(len(os.listdir(self.search_path)))]

        self.get_file(file_dirs)

        for file in self.file_list:
            try:
                data = open(file, "r").read()
                if self.search_text in data:
                    self.result_list.append(file)
                else:
                    pass
            except UnicodeDecodeError:
                try:
                    data = open(file, "r", encoding="utf-8").read()
                    if self.search_text in data:
                        self.result_list.append(file)
                    else:
                        pass
                except UnicodeDecodeError:
                    self.unknown_list.append(file)

        print()
        print("Result:")
        if len(self.result_list):
            for each in self.result_list:
                print(each)
        else:
            print("未检索到符合条件的文件")

        print()
        if len(self.unknown_list):
            print("Unknown:")
            for each in self.unknown_list:
                print(each)


if __name__ == "__main__":
    s = Searcher()
    s.work()
Last modification:April 2, 2020
如果觉得我的文章对你有用,请随意赞赏