Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I don't know how it is an unmatched )

from tkinter import *
from tkinter import filedialog


def openFile():
    filepath = filedialog.askopenfile()
    file = open(filepath), 'r')
    print(file.read())
    file.close()



window = Tk()

button = Button(text="Open",command=openFile)
button.pack()

window.mainloop()

C:\Users\Hp\PycharmProjects\pythonProject\venv\Scripts\python.exe "C:\Users\Hp\PycharmProjects\pythonProject\open a file.py"
File "C:\Users\Hp\PycharmProjects\pythonProject\open a file.py", line 7
file = open(filepath), ‘r’)
^
SyntaxError: unmatched ‘)’

Process finished with exit code 1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From the documentation of tkinter.filedialog.askopenfile:

…create an Open dialog and return the opened file object(s) in read-only mode.

So what filedialog.askopenfile() returns isn’t a file path (you’d use askopenfilename for that), but the file object you can read from:

def openFile():
    file = filedialog.askopenfile()
    print(file.read())
    file.close()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading