0

I tried to make my first bigger python project to learn python. It should be a text editor but I struggle with the filedialog to open a file. I did a lot of research here and elsewhere on the internet, did debugging and even ran about 4 code checker which I found on the internet through without finding the error. For the error in the ("All files", "* .*") where I get the error (code and error below) I tried the following changes

  • "."
  • ".*"
  • '*. *'
  • '.*'

none of them seems to work. I also checked an online tutorial which does a text editor too. The same code seems to run for him but not for me. Idk. I am sure it is a stupid small error but I just cant find it and hope someone can help me out. I added the part of the code that makes the problem including the main structures but not the full code so it is not too confusing.

from distutils import command, text_file
from tkinter import *
from tkinter import filedialog
from tkinter import font

root = Tk()
root.title('Karins Text Editor')
root.geometry("1200x660")

#create new file function
def new_file():
    my_text.delete("1.0", END) #first line in text editor is defined as 1.0
    root.title('New File - Karins Text Editor') #update status bar
    status_bar.config(text="New File           ")

    #open file
    def open_file():
        #delete previous text
            my_text.delete("1.0", END) #first line in text editor is defined as 1.0
    
            #grab Filename
            text_file = filedialog.askopenfilename(initialdir="C:\Users\ka_de\Documents", title="Open File", filetypes=(("Text Files", "*.txt"), ("HTML Files", "*.py"), ("all files", "*.*")))
            name = text_file
            status_bar.config(text=f'{name}        ')
            name=name.replace("C:/gui/", "")
            root.title(f'{name} - Karins Text Editor')

file_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="File", menu=file_menu) #gives me at the top a menu that says file, now create the stuff that pops up when i click on file
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open")
    

root.mainloop()

the error is the following

File "c:/Users/ka_de/OneDrive/Desktop/Coding/100 projects/Py_TextEditor/PyText.py", line 42
text_file = filedialog.askopenfilename(initialdir="C:\Users\ka_de\Documents", title="Open File", filetypes=(("Text Files", ".txt"), ("HTML Files", ".py"), ("all files", "* .*")))

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

VS Code seems to have a problem with the Quotation Marks. I know it is probably an easy fix I should have been able to see and figure out even as a Noobie but I was just not able to and am desperate.

Thanks so much for the help in advance. Much appreciated

Random Davis
  • 6,662
  • 4
  • 14
  • 24
KDeutsch
  • 25
  • 6
  • `"C:\Users\ka_de\Documents"` => `r"C:\Users\ka_de\Documents"` (or `"C:\\Users\\ka_de\\Documents"`). – ekhumoro Feb 23 '22 at 17:52
  • i tried those all it still doesnt work though – KDeutsch Feb 24 '22 at 21:42
  • What do you mean by "doesn't work"? Are claiming that it still produces the same error - or what exactly? – ekhumoro Feb 24 '22 at 22:44
  • yes. it still gives me the same error. doesn't matter how I write the path. – KDeutsch Feb 26 '22 at 14:33
  • If it gives the same error, you aren't using the code I posted. If you start a python interactive session and enter `"C:\Users\ka_de\Documents"`, it will raise the same error shown in your question. But if you enter `r"C:\Users\ka_de\Documents"` or `"C:\\Users\\ka_de\\Documents"`, there is no error. So it most certainly does matter how you write the path. – ekhumoro Feb 26 '22 at 17:10

0 Answers0