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

TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper in tkinter

I’m trying to run sample tkinter a code but showing an error

this is the code that i tried,

def save_as():
    global fo
    fo = asksaveasfile(initialfile = 'Untitled.txt', defaultextension=".txt",filetypes=[("All Files","*.*"),("Text Documents","*.txt"),("Word files",".docx")])
    print(os.path.basename(fo).split('/')[-1])
def sample():
    btn = Button(root, text='click', command=save_as)
    btn.pack()
root = tk.Tk()
sample()

root.mainloop()

But i geting an error like this

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

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:\Users\DELL\Desktop\Ismail\test.py", line 9, in save_as
    print(os.path.basename(fo).split('/')[-1])
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\ntpath.py", line 242, in basename
    return split(p)[1]
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\ntpath.py", line 211, in split
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper

can anyone help

>Solution :

Note that fo is an open file object, so you cannot use it in os.path.basename().

Use asksaveasfilename() instead of asksaveasfile().

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