0

i have a python class that allow the user to select list of files then read the files and search for requested word.

the problem is i am able to select files and read but i am not able to search for word.

i will display the functions :

  • preview the selected file
  • read the file
  • search for word

the code display a windows that includes

  • edit text for extention
  • button for open file dialog
  • listWidget that includes the list of files
  • edit text for enter searching word
  • button for run the searching
  • edit text that display the read file

code:

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import (QApplication, QCheckBox, QColorDialog, QDialog,
                                 QErrorMessage, QFileDialog, QFontDialog, QFrame, QGridLayout,
                                 QInputDialog, QLabel, QLineEdit, QMessageBox, QPushButton)

    from PyQt5.QtCore import QDir, Qt
    from PyQt5.QtWidgets import *

    import os,time,re
    import pdfviewer


    class pdfViewer(pdfviewer.Ui_PdfPreviewWindow):

        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)
        '''
        search for entered string using regular expression in the lineEditSearch 
          ==> highlight the requested word
          ==> display number of occurence of the searched word 
        '''
        def searchWord(self,selectedFile):
            fileToSearchInside = self.readFile(selectedFile)
            searchedSTR = self.lineEditSearch.text()

# i think the error is here but i do not know how to fix it 

            while fileToSearchInside>0:
                try:
                    if(searchedSTR in fileToSearchInside):
                        print("matched string")
                except Exception as e:
                    print(e)
        '''
        read file based on the user click 
        '''
        def readFile(self, currentFile):
            currentFile = self.listWidgetPDFlist.currentItem().text()
            print(currentFile)
            try:
                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))     

        '''
        get the name of the current item selected in the list (==>the path name )
           set  in the edit text  the selected item (==> selected file)
        '''
        def previewSelectedFile(self):

            Item=self.listWidgetPDFlist.currentItem().text()
            self.textEdit_PDFpreview.setText(self.readFile(Item))
Pyt Leb
  • 99
  • 2
  • 10

2 Answers2

0

the problem was in the logic of the code.

so after the system read the content of text and assigned to a variable

we just need to make a condition in order to check if the requested word exist or not.

so the solution remove the while loop. from inside the searchWord function

Pyt Leb
  • 99
  • 2
  • 10
0

You have an infinite loop here:

while fileToSearchInside>0:
    try:
        if(searchedSTR in fileToSearchInside):
            print("matched string")
    except Exception as e:
        print(e)

file.read(n) reads n number of characters from the file, or if n is blank it reads the entire file. So you can change the lines above in:

if searchedSTR in fileToSearchInside:
    print("matched string")

In order to match the requirements showed into the comments above the function you can insert:

import re
occ_founded = re.findall(searchedSTR, fileToSearchInside, re.M)

In this way you can have a list of word founded into the file and so the number of these occurrences

S. Galati
  • 58
  • 10