2

I'm trying to post the text from a .txt file in text screen on a Tk window, but every time I click on the the file to display I get an error that says FileNotFoundError: [Errno 2] No such file or directory: 'dffffff.txt' How do I fix this error?

Here is what I tried.

v_entry = tkinter.Tk(className=' View Entrys' )
        v_entry.geometry("400x200")
        dir_path = (r'C:/Users/crist/OneDrive/Desktop/Express Main/Express Entrys/')
        vlist = os.listdir(dir_path)
        lbox = tkinter.Listbox(v_entry)
        lbox.place(x=268, y=1)
        for item in vlist:
            lbox.insert(tkinter.END, item)
        def scont(event):
            x = lbox.curselection()[0]
            file = lbox.get(x)
            with open(file) as file:
                configfile.insert(INSERT, file.read())
            text.delete('1.0', tkinter.END)
            text.insert(tkinter.END, file)
        text = tkinter.Text(v_entry, bg='cyan')
        text.pack()
        lbox.bind("<<ListboxSelect>>", scont)
        v_entry.mainloop()
  • 1
    "No such file or directory" means exactly what it says - you're giving it an invalid path. – Bryan Oakley Dec 25 '22 at 04:10
  • How am I giving it an invalid path when the title of the text file shows up? It just will not display the text in it? – Cristan Erwin Dec 25 '22 at 04:14
  • 1
    The first step is to examine `file` immediately before the `with open(file)` statement. Verify that it is what you expect, and verify that the file exists in the current working directory or at the absolute path if it's absolute. – Bryan Oakley Dec 25 '22 at 05:11
  • Does your program exist in dir_path? – Derek Dec 25 '22 at 08:59

2 Answers2

0

FileNotFoundError: [Errno 2] No such file or directory: Means that you're trying to access a path/file which does not exist.

Simply creating the directory should remove the error. You can recursively create a directory in powershell using this command:

New-Item C:/Users/crist/OneDrive/Desktop/Express Main/Express Entrys/ -ItemType Directory
Shakir
  • 270
  • 2
  • 11
0

Check the dffffff.txt file. FileNotFoundError: [Errno 2] No such file or directory show when the file is not at the path.

Lim
  • 7
  • 5