0

I've been using auto py to exe to convert my program into an exe file. however, I can't make it open the txt files that the program uses.

My program looks like this:

With open ("text.txt", "r") as file:
    """does something"""

and every time I run the .exe file I get this error:

No such file or directory: 'text.txt'

I am using the -add-data commando, though. So, for what I can see in the library manual, it should work. I had the same problem with PyInstaller. any help?

David
  • 1
  • Hi David, welcome to Stack Overflow. To help others help you, it's useful to include a minimal reproducible example of the issue you are trying to solve. Since this is with auto-py-to-exe, it would be useful to see the "current command" that auto-py-to-exe is building for you. – dbc Jan 18 '22 at 21:58
  • Also note that auto-py-to-exe uses PyInstaller under the hood, and there are many questions on SO about including data files with PyInstaller. For example: https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile/13790741 – dbc Jan 18 '22 at 22:01

2 Answers2

0

Have updated previous answer , manually place the txt file in the output folder generated

import os
import sys


try:
    with open(os.path.join(sys.path[0], "text.txt"), "r") as f:
        print(f.read())
except Exception as e:
    print(e)
0

I found that when you using auto-py-to-exe create --onefile exe. When the exe file runs, it creates a folder in C:\Users{user}\AppData\Local\Temp and runs it. You can using print(str(Path(__file__).parent.absolute())) to check

ThienLv
  • 1
  • 2