Open and display a file through a function click and using the opened file as input for another function with PYQT5
-
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)
-
@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.
-
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.
-
@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".
-
@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. -
@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
-
@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.
-
@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.
-
@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.
-
@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)