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

Loading file with Tkinter returns IO error

So I am working on a tool which involves saving a file with Hex data.
What I want to happen is you save the file and it overwrites that file with stored hex data. However, I get an error which reads:

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

I am using Tkinter’s asksaveasfile to save the file, yet its class is loaded as <class '_io.TextIOWrapper'>, when I want it to be <class 'bytes'>. How would I solve this?

#Saving file
def savefile():
    filetypes = (
        ('level files', '*.level')
    )

   global content

   print(levelIcon.get()[0] + levelIcon.get()[1])

   savedFile = fd.asksaveasfile(defaultextension='.level',
                                filetypes= [('Level','.level')])
   if savedFile is None or filename is None or content is None:
       return

   print(type(savedFile))
   print(savedFile)
   print(content[1])
   with open(savedFile,"wb") as newFile:
       newFile.write(contents)
       newFile.close()

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 :

If you check the documentation, you’ll see that asksaveasfile does not return a filename. It returns an opened file handle. You just need to write to it.

savedFile = fd.asksaveasfile( ... )
savedFile.write(contents)
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