Skip to content Skip to sidebar Skip to footer

Valueerror: Invalid File Path Or Buffer Object Type:

Here is a simplified version of some code that I have. In the first frame, the user selects a csv file using 'tk.filedialog' and it is meant to be plotted on the same frame on the

Solution 1:

I haven't run your 'simplified' version of the code because it's by no means a Minimal, Complete, and Verifiable example.

The error tells you that you're assuming something is a path or buffer when it is StringVar. I believe the error is on the line:

df = pd.read_csv(filename, encoding='latin-1')

this requires filename to be a path or buffer object where as on the very line above filename is indeed a StringVar object:

filename = tk.StringVar()

df = pd.read_csv(filename, encoding='latin-1')

In order to reach the value of StringVar or any of the Variable subclass types, one needs to use get method.

filename.get()

However, that would result an empty string, '' which would raise another error.

Solution 2:

I had a very similar problem with PyQt5. Not sure what was going on, I used print(filename) and found that the string included the filter information (i.e. ("*.csv").

Using:

savePath, _filter = QtWidgets.QFileDialog.getSaveFileName(None, "Some Title", "someFile.csv", "CSV files (*.csv)")

the filter info was stripped off and saving worked without error.

Solution 3:

I ran into this problem with PyQt5 and opening a file. Found that when in my case using

filename = QFileDialog.getOpenFileName(self, 'Open file', '', 'csv(*.csv)')

that the file dialog reads in the filename + , 'csv(*.csv)')

To get just the filename from something like QFileDialog or something similar you can grab the first value in that 'filename'

file = filename[0]

Solution 4:

This also happens when you initialize file name inside the constructor and pass it over to a function.

classMainWindow(QtWidgets.QMainWindow):
    send_fig = QtCore.pyqtSignal(str)

    def__init__(self, plot_file=None):
        super(MainWindow, self).__init__()
        .
        .
        .

        self.plot_file = "./filename.csv"
        self.plot(self.plot_file)

    defplot(self, file_name):
        print(file_name)
        df = pd.read_csv(file_name, encoding='utf-8', engine='python')

This raises an error:

raise ValueError(msg.format(_type=type(filepath_or_buffer))) ValueError: Invalid file path or buffer object type: class 'int'

classMainWindow(QtWidgets.QMainWindow):
    send_fig = QtCore.pyqtSignal(str)

    def__init__(self, plot_file=None):
        super(MainWindow, self).__init__()
        .
        .
        .

        self.plot(self.plot_file)

    defplot(self, file_name):
        print(file_name)
        # file_name = "./filename.csv"
        df = pd.read_csv(file_name, encoding='utf-8', engine='python')

This shoud work fine, either you define file name in the function plot or simply pass it the constructor as:

file_name = "./filename.csv"
app = QtWidgets.QApplication(sys.argv) 
ex = MainWindow(file_name)
sys.exit(app.exec_())

Post a Comment for "Valueerror: Invalid File Path Or Buffer Object Type: "