Home > Net >  Loading file with Tkinter returns IO error
Loading file with Tkinter returns IO error

Time:04-15

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()

CodePudding user response:

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)
  • Related