I have a 'treatment.py' module, which needs 2 files to be input: defects.csv and data.csv. Instead of directly writing the path, I decided to use the QFileDialog dialog from PyQt5. When I select both files, the following error occurs:

Traceback (most recent call last): File "D:/...", line 63, in <module> tr.treat(defects, data) File "D:\Working\ML\Трещины CD\treatment.py", line 16, in treat def90_18 = pd.read_csv(p_def90_18, sep=';', encoding='latin-1') File "C:\Python\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f return _read(filepath_or_buffer, kwds) File "C:\Python\lib\site-packages\pandas\io\parsers.py", line 413, in _read filepath_or_buffer, encoding, compression) File "C:\Python\lib\site-packages\pandas\io\common.py", line 232, in get_filepath_or_buffer raise ValueError(msg.format(_type=type(filepath_or_buffer))) ValueError: Invalid file path or buffer object type: <class '__main__.App1'> 

I use the following dialog box code:

 import sys from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog import treatment as tr class App1(QWidget): def __init__(self): super().__init__() self.left = 500 self.top = 300 self.width = 640 self.height = 480 self.initUI() def initUI(self): self.setGeometry(self.left, self.top, self.width, self.height) self.openFileNameDialog1() self.show() self.close() def openFileNameDialog1(self): global defects options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog defects, _ = QFileDialog.getOpenFileName(self,"Выберите файл с \"defects\" ", "", '(*.csv)', options=options) if defects: return defects class App2(QWidget): def __init__(self): super().__init__() self.left = 500 self.top = 300 self.width = 640 self.height = 480 self.initUI() def initUI(self): self.setGeometry(self.left, self.top, self.width, self.height) self.openFileNameDialog2() self.show() self.close() def openFileNameDialog2(self): global data options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog data, _ = QFileDialog.getOpenFileName(self, "Выберите файл \"data\" ", "", '(*.csv)', options=options) if data: return data if __name__ == '__main__': app = QApplication(sys.argv) defects = App1() data = App2() tr.treat(defects, data) sys.exit(app.exec_()) 

What am I doing wrong?

  • And if so: defects = App1().openFileNameDialog2() and it is unclear why you are making such a call inside the global defects method? - Alexander Chernin
  • I made global defects so that defects and data are available for tr.treat (defects, data). If you do 'defects = App1 (). OpenFileNameDialog2 ()', then two windows are called. But, I did not quite understand ... everything is assigned to the single variable 'defects', but you need 2: defects and data. - Sergey Ershov
  • it is not necessary you return value. Um, in short, everything has to be redone ... Now I’ll get to the coupon - Alexander Chernin
  • Do you always have the names of the files defects.csv and data.csv? Do the App1 and App2 classes necessarily have to be, or are there enough dialog boxes for selecting files? - Alexander Chernin
  • I need defects and data in order to file tr.treat (). I tried to redo it, but for some reason the window is called 6 times (3 for openFileNameDialog1 and also 3 for openFileNameDialog2. - Sergey Ershov

2 answers 2

Or even the fourth):

 import sys from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog import treatment as tr if __name__ == '__main__': app = QApplication(sys.argv) options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog files, _ = QFileDialog.getOpenFileNames(None, "Выберите файлы данных (data.csv) и дефектов (defects.csv)...", "", "csv (*.csv)", options=options) defects = None data = None for(_file in files) if( 'defects' in _file ): defects = _file else if( 'data' in _file ): data = _file if defects and data: tr.treat(defects, data) else: print("Что-то пошло не так [" + defects + "," + data +"]") 
  • By the third option, I meant using the getOpenFileNames function. This is a handy static function that returns one or more existing files selected by the user. And the fourth option? - S. Nick
  • @ S.Nick he is in front of you - Alexander Chernin
  • one
    My 4th is working fine) S. Nick and Alexander Chernin, Thank you very much for your help. - Sergey Ershov
  • @ Sergey Ershov great. With great pleasure I will accept a green tick as a gift)) - Alexander Chernin

Try an example, in my opinion you need a third option.

 import sys from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog #import treatment as tr class App1(QWidget): def __init__(self): super().__init__() self.left = 500 self.top = 300 self.width = 640 self.height = 480 self.initUI() def initUI(self): self.setGeometry(self.left, self.top, self.width, self.height) # self.openFileNameDialog1() # self.show() # self.close() def openFileNameDialog1(self): # global defects options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog defects, _ = QFileDialog.getOpenFileName(self,"Выберите файл с \"defects\" ", "", '(*.csv)', options=options) if defects: return defects class App2(QWidget): def __init__(self): super().__init__() self.left = 500 self.top = 300 self.width = 640 self.height = 480 self.initUI() def initUI(self): self.setGeometry(self.left, self.top, self.width, self.height) # self.openFileNameDialog2() # self.show() # self.close() def openFileNameDialog2(self): # global data options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog data, _ = QFileDialog.getOpenFileName(self, "Выберите файл \"data\" ", "", '(*.csv)', options=options) if data: return data ### vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv class OpenSheet(QFileDialog): def __init__(self): super().__init__() self.setWindowModality(True) self.setFileMode(self.ExistingFiles) self.setAcceptMode(QFileDialog.AcceptOpen) class App3(QWidget): def __init__(self): super().__init__() self.setGeometry(500, 300, 640, 480) def openFileNameDialog3(self): diag = OpenSheet() options = diag.Options() options |= diag.DontUseNativeDialog files, _ = diag.getOpenFileNames(None, "Выберите файл \"data\" )", "", "Csv Files (*.csv)", options=options) if files: print("Selected files: ", files) return files # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ if __name__ == '__main__': app = QApplication(sys.argv) d1 = App1() defects = d1.openFileNameDialog1() print("defects", defects) d2 = App2() data = d2.openFileNameDialog2() print("data", data) d3 = App3() files = d3.openFileNameDialog3() print("Выбраны фалы: ", files) # tr.treat(defects, data) # sys.exit(app.exec_()) 

enter image description here

  • Added the third option. Each file asks to choose 2 times (or press open). The fourth option is for the 2nd python? - Sergey Ershov
  • @ Sergey Yershov did you run my example or have you upgraded yours? If your then delete the lines self.openFileNameDialog1() and self.openFileNameDialog2() - S. Nick
  • Own Ok, I will try thanks. - Sergey Ershov