0

I just learned the basics of pyinstaller. I am making a simple to do list app and to save data it uses a text file. The program will not run without the text file. I have tried to add the text file with "pyinstaller ToDoList.py --add-data "List.txt:." --windowed" but it gives this error in the terminal when I run it:

"Traceback (most recent call last):

File "ToDoList.py", line 3, in < module>

FileNotFoundError: [Errno 2] No such file or directory: 'List.txt' [6628] Failed to execute script 'ToDoList' due to unhandled exception!"

I have checked Pyinstaller adding data files for help but the help there did not work. If it makes any difference I use a mac. Could someone help me please? I know it's a lot to ask but it would help me so much. Thanks for your time, effort, and consideration.

My code:

import tkinter as tk
from tkinter import *
open = open("List.txt", "r+")
file = open.readlines ()
def main ():
    window = Tk()
    window.title("To Do")
    def window_destroy ():
        window.destroy()
    window.geometry ("1440x808+0+0")
    to_do_label = Label(window, text="To Do List:", font=("Times New Roman",25,))
    to_do_label.place(relx=.5, y=20, anchor=CENTER)
    to_do_listbox = Listbox(window, width=50, height=20)
    for i in range(len(file)):
        to_do_listbox.insert(tk.END, str(file[i].strip()))
    to_do_listbox.place(relx=.5, y=230, anchor=CENTER)
    def delete_selected_item ():
        to_do_listbox.delete(tk.ANCHOR)
        for i in range(to_do_listbox.size()):
            open.truncate(i)
        for i in range(to_do_listbox.size()):
            open.write(to_do_listbox.get(i) + "\n")
    add_entry = Entry(window, width=50, font=("Times New Roman", 15))
    add_entry.place(relx=.5, y=425, anchor=CENTER)
    def add_entry_to_list ():
        to_do_listbox.insert(tk.END, add_entry.get())
        for i in range(to_do_listbox.size()):
            open.truncate(i)
        for i in range(to_do_listbox.size()):
            open.write(to_do_listbox.get(i) + "\n")
    add_button = Button(window, text="Add item", width=10, font=("Times New Roman", 15), command=add_entry_to_list)
    add_button.place(relx=.5, y=455, anchor=CENTER)
    delete_button = Button(window, text="Delete item", width=10, font=("Times New Roman", 15), command=delete_selected_item)
    delete_button.place(relx=.5, y=485, anchor=CENTER)
    quit_button = Button(window, text = "Quit", width=5, font=("Times New Roman",15), command=window_destroy)
    quit_button.pack(anchor = "s", side = "right")
    window.mainloop()
main ()
Moose
  • 5
  • 4
  • 1
    You need `--add-data` before `"src:List.txt"`, i.e. `pyinstaller ToDoList.py --add-data "src:List.txt" --windowed`. – acw1668 Jan 07 '22 at 03:18
  • Also the argument for `--add-data` should be `"List.txt:."` instead. – acw1668 Jan 07 '22 at 07:49
  • When I put "pyinstaller ToDoList.py --add-data "List.txt:." --windowed" into the terminal prompt I get the .app bundle but when I run the application it doesn't open and when I go into the files I get: – Moose Jan 07 '22 at 12:48
  • "Traceback (most recent call last): File "ToDoList.py", line 3, in FileNotFoundError: [Errno 2] No such file or directory: 'List.txt' [6122] Failed to execute script 'ToDoList' due to unhandled exception: [Errno 2] No such file or directory: 'List.txt' [6122] Traceback: Traceback (most recent call last): File "ToDoList.py", line 3, in FileNotFoundError: [Errno 2] No such file or directory: 'List.txt'" – Moose Jan 07 '22 at 12:48
  • This means that it still did not include the file in the bundle – Moose Jan 07 '22 at 12:49
  • `--add-data "List.txt:."` works for me. Did you change to directory of the executable before executing it? – acw1668 Jan 07 '22 at 14:49
  • the folder "MyApp" looks like this: ToDoList.py, setup.py, List.txt, ToDoList.spec, pycache, dist, build. They are all in the same folder and my computer has the directory from cd MyApp. I put pyinstaller ToDoList.py --add-data "List.txt:." --windowed into the terminal but it didn't work. The text file has constantly changing data if that means anything. – Moose Jan 07 '22 at 15:28

0 Answers0