Как искать строку на всем диске с помощью свиста и питона? - PullRequest
0 голосов
/ 12 апреля 2019

Я хочу найти строку или часть из строки на сетевом приводе как можно быстрее.

Я играл со свистом, но не получил правильный результат. Я получаю только ошибки разрешения.Я хочу, чтобы, если файл не читался, программа продолжала работать до тех пор, пока не будет произведен поиск всего диска.

import os
from whoosh.index import create_in
from whoosh.fields import Schema, TEXT, ID


def createSearchableData(root):

    '''
    Schema definition: title(name of file), path(as ID), content(indexed
    but not stored),textdata (stored text content)
    '''
    schema = Schema(title=TEXT(stored=True),path=ID(stored=True),\
              content=TEXT,textdata=TEXT(stored=True))
    if not os.path.exists("indexdir"):
        os.mkdir("indexdir")

    # Creating a index writer to add document as per schema
    ix = create_in("indexdir",schema)
    writer = ix.writer()


    filepaths = [os.path.join(root,i) for i in os.listdir(root)]
    for path in filepaths:
        try:
            fp = open(path,'r')
            text = fp.read()
            if "L" in text:
                print("Found the search data in:",path)
            writer.add_document(title=path.split("\\")[1], path=path,\
            content=text,textdata=text)
            fp.close()
            writer.commit()
        except Exception as e:
            print("ERROR",e,path)

root = "Y:\Products ABC"
createSearchableData(root)
...