So I was just building an IDE in python using tkinter. When I tried to open a .py file in it using tkinter.filedialog's askopenfilename, which contained characters that are not present in keyboard (which I got by clicking win_key + period), it didn't open and threw an error:
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 9588: character maps to <undefined>
I am sure it is not able to read the symbol ← and →.
My code for opening the file:
def open_function(event=None):
if saved == False:
file_not_saved = askyesnocancel("File not saved", "Your file is not saved. Do you want to save your file?")
if file_not_saved == 1:
save_function()
elif file_not_saved == None:
return
path = askopenfilename(title="Open File", filetypes=[("Python files (*.py)", "*.py")])
if path != "":
with open(path, "r") as file:
code = file.read()
editing_area.delete("1.0", "end")
editing_area.insert("1.0", code)
set_file_path(path)
set_file_name(basename(path))
set_saved(True)
elif path == "":
showerror("Error", "Some file path error occured. Please open your file again.")
So my question obviously is how to make python's with open function read out-of-keyboard characters.
I am using python 3.10.1
Any help would be appreciated!!
So I searched on the internet about it and found that encoding="utf-8"
does it. The default encoding doesn't detect symbols like ← and →.
What I did change:
def open_function(event=None):
if saved == False:
file_not_saved = askyesnocancel("File not saved", "Your file is not saved. Do you want to save your file?")
if file_not_saved == 1:
save_function()
elif file_not_saved == None:
return
path = askopenfilename(title="Open File", filetypes=[("Python files (*.py)", "*.py")])
if path != "":
with open(path, "r", encoding="utf-8") as file: # change in this line
code = file.read()
editing_area.delete("1.0", "end")
editing_area.insert("1.0", code)
set_file_path(path)
set_file_name(basename(path))
set_saved(True)
elif path == "":
showerror("Error", "Some file path error occured. Please open your file again.")
It worked for me- Hope it will work for anyone seeing this post in the future....!!