Home > Back-end >  I want tkinter asksaveasfile() to save as utf-8 in python. How can I solve it?
I want tkinter asksaveasfile() to save as utf-8 in python. How can I solve it?

Time:07-09

I wrote the following code in Python:

sveas = f.asksaveasfile()
sveas.write(f'{code.get(1.0,END)}')
sveas.close()

Here I want it to encode the file with utf-8. how can i solve

CodePudding user response:

If you want to specify the encoding type, you need to pass encoding='utf-8' when you open the file. Since asksaveasfile() will open the file implicitly for you but you cannot specify the encoding type, so asksaveasfilename() need to be used instead and then you open the selected file using open() explicitly:

filename = f.asksaveasfilename()
with open(filename, 'w', encoding='utf-8') as fh:
    fh.write(code.get('1.0', END))
  • Related