0

i have a python class that allow the user to choose a path in order to display the files, then the user enter a searched word in order to match it with all the listed files.

once the user click on the search button i need the system to

  1. read the listed files
  2. override the list by just displaying the files that have matching

the system display this error :

searchWord self.listWidgetPDFlist.addItems(listFiles)

builtins.TypeError: index 0 has type 'bool' but 'str' is expected

for this task i wrote 3 functions

init function

def __init__(self,PdfPreviewObj):
        #QWidget.__init__(self)
        self.PdfPreviewObj =PdfPreviewObj 
        self.setupUi(PdfPreviewObj)
        self.PdfPreviewObj.show()
        self.pushButtonOpenFolder.clicked.connect(self.setExistingDirectory)
        self.pushButtonSearch.clicked.connect(self.searchWord)

readFile function

def readFile(self, currentFile):
    try:
        currentFile = self.listWidgetPDFlist.currentItem().text()
        print(currentFile)

        with open(currentFile) as ctf:
            ctfRead = ctf.read()
            print(ctfRead)
            return(ctfRead)

    except Exception as e:
        print("the selected file is not readble because :  {0}".format(e))     

displayFilteredFile function

    def displayFilteredFiles(self, filteredFiles):
            try:

                filteredFiles = self.listWidgetPDFlist.items()
                print(filteredFiles)
                for  file in filteredFiles:
                    with open(file) as ctf:
                        ctfRead = ctf.read()
                        print(ctfRead)
                    return(ctfRead)

            except Exception as e:
                print("the selected file is not readble because :  {0}".format(e))     

seachedWord function

 def searchWord(self,filteredFiles): 

        listFiles = []
        self.textEdit_PDFpreview.clear()

        self.listWidgetPDFlist.clear()
        listFiles.append(filteredFiles)

       # here the system indicate the error 
        self.listWidgetPDFlist.addItems(listFiles)

        filesToSearchInside = self.displayFilteredFiles(listFiles)
        searchedSTR = self.lineEditSearch.text()
        RepX="<b><span style='color:white;mso-themecolor:background1;background:black;mso-highlight:black'>"+searchedSTR+'</span></b>'

        try:
            textList = filesToSearchInside.split('\n')

            # utility that gives an empty list for each key by default
            d = defaultdict(list)   

            '''
            looping over the selected text and search for the required word in the search box 
            store the word and the number of occurence in a dictionnary  to used for later.
            '''
            for counter, myLine in enumerate(textList):
                thematch=re.sub(searchedSTR,RepX,myLine)
                matches = re.findall(searchedSTR, myLine, re.MULTILINE | re.IGNORECASE)
                if len(matches) > 0:

                    # add one record for the match (add one because line numbers start with 1)
                    d[matches[0]].append(counter + 1)


                    self.textEdit_PDFpreview.insertHtml(str(thematch))
                    #self.textEdit_PDFpreview.insertHtml(result)



                # print out 
            for match, positions in d.items():
                print('{} exists {} times'.format(match, len(positions)))
                for p in positions:
                    print("on line {}".format(p))

        except Exception as e:
                print(e)        
Pyt Leb
  • 99
  • 2
  • 10
  • Where do you use searchWord? you could print filteredFiles? – eyllanesc Mar 31 '18 at 22:01
  • @ eyllanesc filteredFiles suppose to be a list of files that where pre exist in the QlistWidgetItem ==>**listWidgetPDFlist** – Pyt Leb Mar 31 '18 at 22:08
  • i tried your answer it display this error : **builtins.TypeError: 'bool' object is not subscriptable** – Pyt Leb Mar 31 '18 at 22:10
  • I see that you are calling searchword through the clicked signal, so filteredFiles is a Boolean value, and the error you get is to be expected. where do you create the filteredFiles list? – eyllanesc Mar 31 '18 at 22:11
  • You could publish a [mcve], pieces of code does not help us try to fix your code, do not assume, you should verify. – eyllanesc Mar 31 '18 at 22:12
  • i divided the code into pieces in order to make it easier and more understandable .... the **filteredFiles list** is created under the function **displayFilteredFile** where its in my question here the 3rd function – Pyt Leb Mar 31 '18 at 22:15
  • 1. `listFiles.append(filteredFiles)` 2. `self.listWidgetPDFlist.addItems(listFiles)` 3. `filesToSearchInside = self.displayFilteredFiles(listFiles)`, according to what I see a 1 you already access to filteredFiles, and just in 3 you call the function created by filteredFiles. How do you access something before creating it? – eyllanesc Mar 31 '18 at 22:17
  • `self.listWidgetPDFlist.clear()` ... `filesToSearchInside = self.displayFilteredFiles(listFiles)`, It is also eliminating the elements of the QListWidget, so in the function displayFilteredFiles filteredFiles will be an empty list. – eyllanesc Mar 31 '18 at 22:20
  • The separation of your code is not adequate to understand where else the error may be, so in conclusion publish a [mcve] and not pieces of code that are difficult to understand for someone other than you. – eyllanesc Mar 31 '18 at 22:21
  • In every case your exception handling prints the exception but suppresses the stack trace. That makes your problem unnecessarily hard to find. While your are tracking this down, add `raise` at the end of all of your `except` clauses. Time enough to recover gracefully from unexpected errors when you aren't encountering expected ones. – BoarGules Mar 31 '18 at 22:47

0 Answers0