ValueError: Invalid file path or buffer object type: <class 'tkinter.StringVar'> ValueError: Invalid file path or buffer object type: <class 'tkinter.StringVar'> tkinter tkinter

ValueError: Invalid file path or buffer object type: <class 'tkinter.StringVar'>


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.


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.


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]