Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Open and display a file through a function click and using the opened file as input for another function with PYQT5
Forum Updated to NodeBB v4.3 + New Features

Open and display a file through a function click and using the opened file as input for another function with PYQT5

Scheduled Pinned Locked Moved Solved General and Desktop
pyqt5python3
10 Posts 4 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stainopel
    wrote on 22 Nov 2020, 12:32 last edited by Stainopel
    #1

    Good day, I have defined two functions, one to open a .csv file and display the file name, and another to get a file from a specific path and encrypt the file. Both work independently but my goal is to use the file obtained in the first function and use it as input for the second function without having to use a specific path instead as I did. Am having difficulties achieving this, and will appreciate any help you can provide to solve this.
    Here are the functions:

             def getFileEncrypt(self):
                      fname_E, selFilter = QFileDialog.getOpenFileName()
                      if not fname_E:
                          return
                      info = QFileInfo(fname_E)
                      self.lineEdit.setText(info.baseName()) 
              
              def encryption_hash(self,comboBox_2):
                      index = self.comboBox_2.currentIndex() 
              
                      if  index == 1:
                          hasher1 = hashlib.md5()
                          afile1 = open('G:/TestData/0066428-200221144449610/0066428-200221144449610.csv', 'rb')
                          buf1 = afile1.read()
                          a = hasher1.update(buf1)
                          md5 =(str(hasher1.hexdigest()))
                          self.lineEdit_E.setText("File Hash Using MD5: " + md5) 
              
                      else:
                           hasher1 = hashlib.sha1()
                           afile1 = open('G:/TestData/0066428-200221144449610/0066428-200221144449610.csv', 'rb')
                           buf1 = afile1.read()
                           a = hasher1.update(buf1)
                           sha1=(str(hasher1.hexdigest()))
                           self.lineEdit_E.setText("File Hash Using SHA_160: " + sha1)
    
    1 Reply Last reply
    0
    • S Stainopel
      27 Nov 2020, 01:26

      @jsulm Yes, it's true the functions are in the same class, accessing self.fname_E is working but I keep getting this error when I execute the second function

      buf1 = afile1.read()
      
      AttributeError: 'str' object has no attribute 'read'
      

      That's where the problem is. Initially, I read a file from a specific path and applied a hash which worked well. Now, however, the first function seems to be the problem as it's apparently not reading opened files but rather just accessing files and displaying their names. So in effect fname_E is just basically a string and does not contain the opened file equally. So I need help upgrading the first function to hold the opened file and display the filename as a string, whilst ensuring the second function can retrieve the file and use it.

      J Offline
      J Offline
      JonB
      wrote on 27 Nov 2020, 08:17 last edited by
      #9

      @Stainopel
      You consistently seem to be confusing a file name, which is just the name of a file, with some kind of access to the file content, be that an open file handle or perhaps a buffer which has had all the file content read into it. These things are not at all the same, they are not interchangeable.

      There are various ways of handling whatever you are trying to do. But perhaps in the simplest case you might keep two member variables, one for the name, another for the accessed content. At least while you sort out your confusion.

      S 1 Reply Last reply 28 Nov 2020, 21:39
      2
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 22 Nov 2020, 16:20 last edited by mrjj
        #2

        Hi
        Could you not just add new parameter so the function can get it ?

        from
        def encryption_hash(self,comboBox_2):

        to
        def encryption_hash(self,comboBox_2, FileName):
        ...
        afile1 = open(FileName, 'rb')

        But where do you call the getFileEncrypt(self): ?

        If they are part of the same class, you can also just store the filename as a member and both functions would be able to access it.

        S 1 Reply Last reply 22 Nov 2020, 20:44
        2
        • M mrjj
          22 Nov 2020, 16:20

          Hi
          Could you not just add new parameter so the function can get it ?

          from
          def encryption_hash(self,comboBox_2):

          to
          def encryption_hash(self,comboBox_2, FileName):
          ...
          afile1 = open(FileName, 'rb')

          But where do you call the getFileEncrypt(self): ?

          If they are part of the same class, you can also just store the filename as a member and both functions would be able to access it.

          S Offline
          S Offline
          Stainopel
          wrote on 22 Nov 2020, 20:44 last edited by Stainopel
          #3

          @mrjj In this case I don't want the second function to open any file but that it should use the CSV file opened in the first function. The thing is, both functions are connected to different buttons, and I'd want the second button to use the CSV file stored in the variable "fname_E".

          J 1 Reply Last reply 22 Nov 2020, 21:13
          0
          • S Stainopel
            22 Nov 2020, 20:44

            @mrjj In this case I don't want the second function to open any file but that it should use the CSV file opened in the first function. The thing is, both functions are connected to different buttons, and I'd want the second button to use the CSV file stored in the variable "fname_E".

            J Offline
            J Offline
            JonB
            wrote on 22 Nov 2020, 21:13 last edited by
            #4

            @Stainopel
            So make fname_E a member variable, self.fname_E? What exactly is the actual problem here?

            S 1 Reply Last reply 25 Nov 2020, 23:11
            2
            • J JonB
              22 Nov 2020, 21:13

              @Stainopel
              So make fname_E a member variable, self.fname_E? What exactly is the actual problem here?

              S Offline
              S Offline
              Stainopel
              wrote on 25 Nov 2020, 23:11 last edited by
              #5

              @JonB I want to use the .csv file I have opened and stored in the variable fname_E, use it in another function encryption_hash i.e. instead of having 'afile1' open the file specified in the path provided, it should just retrieve and use the file stored in the variable fname_E
              Using self.fname_E like you suggested hasn't solved the problem am afraid.

              J 1 Reply Last reply 26 Nov 2020, 05:58
              0
              • S Stainopel
                25 Nov 2020, 23:11

                @JonB I want to use the .csv file I have opened and stored in the variable fname_E, use it in another function encryption_hash i.e. instead of having 'afile1' open the file specified in the path provided, it should just retrieve and use the file stored in the variable fname_E
                Using self.fname_E like you suggested hasn't solved the problem am afraid.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 26 Nov 2020, 05:58 last edited by
                #6

                @Stainopel I don't understand the problem: if both functions are in the same class they both can access self.fname_E. So, what is the problem?

                def getFileEncrypt(self):
                    self.fname_E, selFilter = QFileDialog.getOpenFileName()
                    if not self.fname_E:
                        return
                    info = QFileInfo(self.fname_E)
                    self.lineEdit.setText(info.baseName()) 
                          
                def encryption_hash(self,comboBox_2):
                    index = self.comboBox_2.currentIndex() 
                    # Here you can also access self.fname_E
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 1 Reply Last reply 27 Nov 2020, 01:26
                3
                • J jsulm
                  26 Nov 2020, 05:58

                  @Stainopel I don't understand the problem: if both functions are in the same class they both can access self.fname_E. So, what is the problem?

                  def getFileEncrypt(self):
                      self.fname_E, selFilter = QFileDialog.getOpenFileName()
                      if not self.fname_E:
                          return
                      info = QFileInfo(self.fname_E)
                      self.lineEdit.setText(info.baseName()) 
                            
                  def encryption_hash(self,comboBox_2):
                      index = self.comboBox_2.currentIndex() 
                      # Here you can also access self.fname_E
                  
                  S Offline
                  S Offline
                  Stainopel
                  wrote on 27 Nov 2020, 01:26 last edited by Stainopel
                  #7

                  @jsulm Yes, it's true the functions are in the same class, accessing self.fname_E is working but I keep getting this error when I execute the second function

                  buf1 = afile1.read()
                  
                  AttributeError: 'str' object has no attribute 'read'
                  

                  That's where the problem is. Initially, I read a file from a specific path and applied a hash which worked well. Now, however, the first function seems to be the problem as it's apparently not reading opened files but rather just accessing files and displaying their names. So in effect fname_E is just basically a string and does not contain the opened file equally. So I need help upgrading the first function to hold the opened file and display the filename as a string, whilst ensuring the second function can retrieve the file and use it.

                  J J 2 Replies Last reply 27 Nov 2020, 07:25
                  0
                  • S Stainopel
                    27 Nov 2020, 01:26

                    @jsulm Yes, it's true the functions are in the same class, accessing self.fname_E is working but I keep getting this error when I execute the second function

                    buf1 = afile1.read()
                    
                    AttributeError: 'str' object has no attribute 'read'
                    

                    That's where the problem is. Initially, I read a file from a specific path and applied a hash which worked well. Now, however, the first function seems to be the problem as it's apparently not reading opened files but rather just accessing files and displaying their names. So in effect fname_E is just basically a string and does not contain the opened file equally. So I need help upgrading the first function to hold the opened file and display the filename as a string, whilst ensuring the second function can retrieve the file and use it.

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 27 Nov 2020, 07:25 last edited by
                    #8

                    @Stainopel Please post your current code. As the error suggests afile1 is a string at the time when you're trying to call read() on it.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • S Stainopel
                      27 Nov 2020, 01:26

                      @jsulm Yes, it's true the functions are in the same class, accessing self.fname_E is working but I keep getting this error when I execute the second function

                      buf1 = afile1.read()
                      
                      AttributeError: 'str' object has no attribute 'read'
                      

                      That's where the problem is. Initially, I read a file from a specific path and applied a hash which worked well. Now, however, the first function seems to be the problem as it's apparently not reading opened files but rather just accessing files and displaying their names. So in effect fname_E is just basically a string and does not contain the opened file equally. So I need help upgrading the first function to hold the opened file and display the filename as a string, whilst ensuring the second function can retrieve the file and use it.

                      J Offline
                      J Offline
                      JonB
                      wrote on 27 Nov 2020, 08:17 last edited by
                      #9

                      @Stainopel
                      You consistently seem to be confusing a file name, which is just the name of a file, with some kind of access to the file content, be that an open file handle or perhaps a buffer which has had all the file content read into it. These things are not at all the same, they are not interchangeable.

                      There are various ways of handling whatever you are trying to do. But perhaps in the simplest case you might keep two member variables, one for the name, another for the accessed content. At least while you sort out your confusion.

                      S 1 Reply Last reply 28 Nov 2020, 21:39
                      2
                      • J JonB
                        27 Nov 2020, 08:17

                        @Stainopel
                        You consistently seem to be confusing a file name, which is just the name of a file, with some kind of access to the file content, be that an open file handle or perhaps a buffer which has had all the file content read into it. These things are not at all the same, they are not interchangeable.

                        There are various ways of handling whatever you are trying to do. But perhaps in the simplest case you might keep two member variables, one for the name, another for the accessed content. At least while you sort out your confusion.

                        S Offline
                        S Offline
                        Stainopel
                        wrote on 28 Nov 2020, 21:39 last edited by
                        #10

                        @JonB Very Good Suggestion, I used a second variable in the other function to read the opened file(s) from the first function and it did work like a charm. Thanks. Here is an update to the code

                        def encryption_hash(self,comboBox_2):
                                
                                index = self.comboBox_2.currentIndex() 
                                
                                path = self.fname_E
                                
                                with open(path, 'rb') as f:
                                
                                    if  index == 2:
                                        hasher1 = hashlib.md5()
                                        afile1 = f
                                        buf1 = afile1.read()
                                        a = hasher1.update(buf1)
                                        md5 =(str(hasher1.hexdigest()))
                                        self.lineEdit_E.setText("File Hash Using MD5: " + md5)
                        
                        1 Reply Last reply
                        0

                        6/10

                        26 Nov 2020, 05:58

                        • Login

                        • Login or register to search.
                        6 out of 10
                        • First post
                          6/10
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved